How do I insert on LINQ an entity with the other mapping entities as well?
Posted on 2009-02-18
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".