views:

122

answers:

2

Hi,

i can't seem to find the answer to this.... i have some data coming from other tiers and it represents a EF object. When it's new, i do this:

context.AddToCustomer(mynewobject);
context.SaveChanges();

but now my data forms an existing object, so i want the context to know i want to update the data and not inserting it.

i've seen 'ApplyPropertyChanges' but i can't figure out how to use it. I've also seen people doing this: Customer existingOne = (from n in context.Customers where n.id = mynewobject.id select n).First()

existingOne.name = mynewobject.name
existingOne.address= mynewobject.address
context.SaveChanges();

but that seems a little odd because i have to manually set all the props AND read the entire object first.

Michel

+2  A: 

While I question whether it's even worthwhile to "optimize" an update, you can nevertheless do what you ask. It's easier in EF 4, but also possible in EF 1. See also this article.

Craig Stuntz
Just to add that things get a bit more complicated with EF1 when you want to update other properties then scalar (as you are going to be able to see at "Caveats" section of "possible in EF1" article). You can find workaround for that between answers to this question http://stackoverflow.com/questions/1612655/entity-framework-updating-with-related-entity
Misha N.
Thanks very much for the answer. Sometimes you just have to see it to believe it. I couldn't have figured this out myself, the 'possible in EF 1' solution.
Michel