views:

279

answers:

2

I'm returning a List (own class) from Silverlight to a service, in this list there are old entities with updated values and completely new entities, how do I save this back to the database using the entity framework?

Using cEnts.CardItems.AddObject gives me duplicates obviously.

+1  A: 

You want to use Attach() instead of AddObject().

Attach will take your disconnect object and let the container know to consider it for updates. The new objects, without a PrimaryKey, will be added.

jfar
Here's a code example: http://blogs.msdn.com/alexj/archive/2009/06/19/tip-26-how-to-avoid-database-queries-using-stub-entities.aspx
Craig Stuntz
A: 

If you are using the same entity context for selecting and update/insert you have to call AddTo...() method to insert the new entities and ApplyPropertyChanges to the changed ones.

If you are using different contexts the problem is more complicated because you have to detach entities from one context and attach them to another. Once detached entities lose their changed state and you have to explicitly specify which properties have been changed (For more info check this: http://www.abadjimarinov.net/blog/2009/12/13/AttachAlreadyChangedObjectToADataContextInEntityFramework.xhtml ).

Branislav Abadjimarinov