Entity Framework 4 POCO update with disconnected objects
Posted on 2011-03-14
I have something still not clear about updates in EF 4 with POCO classes (disconnected object).
I have let's say an objet called user and a graph like this:
user.Orders (collection of orders)
user.Profile (object profile)
user.Friends (collection of friends)
and so on
I can add or delete, but my problem starts when I need to update.
What I usually do is to retieve the original entity from the context (with the navigation properties) and then I iterate through the object graph in order to manually update the properties for all the navigation properties as well.
Sometime happens that the object contains some object to add and not to update.
For istance the user can contains a new order, and so I need to add the new order and not update (But for the user itself it's an update).
Does exist a flexible way to have a generic function where I can pass the user or even better a generic entity (that can contains elements to update and/or elements to add) and EF understands what to add or what to update?
public ResultObject UpdateUser(User user)
//context analyzes the object user with navigation properties (collection as well)
//and add or updates everything automatically
If doesn't exist a way for that can someone explain me the concept for updating disconnected object with navigation properties to update or add?
P.S. I can't use self tracking entities
Thanks in advance