How do I insert on LINQ an entity with the other mapping entities as well?

Posted on 2009-02-18
Last Modified: 2013-11-11
I have a problem. I have this screen that will allow the users to create a new Employee. That employee will have addresses. On the screen the users supply the employee information and each address they want to add. They can look for an existing address and say that the employee works or lives at the existing address. Now. I have 3 tables


What I want to do is insert the new employee with MyDataContext.Employees.InsertOnSubmit(MyEmployee); My employee has all the "EmployeeAddress" entities for each address in there. I tried creating a new EmployeeAddress and adding it to the Employee.EmployeeAddresses colection and blows up with "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported." After that I changed it so it wouldn't add the whole address to the EmployeeAddress entity just the Id. And then it blows up with "Cannot add an entity with an id that already exists".

Any Ideas?

Question by:aisinfo
    LVL 62

    Expert Comment

    by:Fernando Soto
    Please show the code.
    LVL 62

    Expert Comment

    by:Fernando Soto
    Also the relationship between the tables.

    Accepted Solution

    There was a problem with the database, one of the Id wasn't set to self increment.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now