I set up an mvc application using the EF4 model and POCO T4 template. As you probably know this result in me having class with children defined in them. I then generate the controller for the parent entity resulting in the views(createAndEdit partial, create, edit, details, delete and index) and the repository class.
This far everything is fine. Ican retrieve entities and thier children and saving sinlge entities to the database.
I can creat a "new" entity and add "new" children to it and when it runs throught the inserOrUpdate repository code everything is good. Both the parent and the children get created.
I retireve an "old" parent entity from the database and then add "new" children to the parent by parent.Children.Add(newChild).
Here is where my problems start:
- If i call the update repository class passing in the parent then the paret is updated but none of the children are saved to the database.
If more than one "new" child is added to the parent then the follow error is throw when the insertOrUpdate method tries to attach the parent object "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." I think this is due to the children having the same primary key value (zero) as this created by the database.
I think this is a bug, but if this is not then could you tell me what i am doing wrong?