Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update A Collection With Entity Framework

I have a detached set of client objects that I'd like to update (I know they already exist in the db by primary key). Now I want to update them to the database. Knowing I need to query them first, I do so and now have to basically take the properties from the deattached objects and apply them to the attached objects. I finally call save changes. Is there a slick way to apply these properties from the detached collection to the attached one?

NOTE: The detached objects don't have the primary keys in them but I do have enough information to link with via a comparer class.

like image 978
RailRhoad Avatar asked Nov 06 '22 17:11

RailRhoad


1 Answers

You don't need to do what you're doing. You can just call the Attach method on your ObjectContext to tell it that you want to work with your detatched objects. Then just call SaveChanges to update the database with your changed objects.

like image 77
David Pfeffer Avatar answered Dec 25 '22 13:12

David Pfeffer