• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 662
  • Last Modified:

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

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?

  • 2
1 Solution
Fernando SotoCommented:
Please show the code.
Fernando SotoCommented:
Also the relationship between the tables.
aisinfoAuthor Commented:
There was a problem with the database, one of the Id wasn't set to self increment.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now