How to save collection into entity properly?
I have some object with EntityCollection inside. If I just add the new entity everything works good. The code that does it here:
Context.ApplyChanges()
Context.SaveChanges()
But if I try to update the entity I have the following message:
开发者_开发问答AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.
Even if use the same collection objects. I mean there can not be real the same entity objects in the collection because are saved into database if they are new. But! If I try to apply the same code to the separate entities into collection they are saved but again if I try to apply it on the object (container) I have this error.
Thanks for the help
Have you tried retrieving the entity you wish to change from the Context
, modifying that entity appropriately, then saving changes? If so, could you provide a more detailed example?
I suspect you've omitted something from your model or your table schema. This is what I would expect if the model primary key was expected to be autogenerated, but your table/model code doesn't define it that way. You'd then have all the primary keys for the object in the collection set to zero and you get a primary key violation when attempting to insert the second object.
精彩评论