Answered by:
Update detached POCO with modified relationships

Question
-
Hi,
I'm struggling with POCO update through ObjectDataSource using FormView;
the POCO is in detached state so I have to whire this:
var context = ((IObjectContextAdapter)HRDb.Instance).ObjectContext;
context.ObjectStateManager.ChangeObjectState(department, EntityState.Modified);
so I'm reattaching the department entity here;
this is ok but the proble is that it only updates the primitive properties this way, not the relations
my detachedEntity has property like department.ParentDepartment and this relation remainc unchanged.
when I try this
context.ObjectStateManager.ChangeRelationshipState(department, department.ParentDepartment, "ParentDepartment", EntityState.Modified);
it says that the relation cannot be in the modified state.
So what should I do to update the reattached entity "completely"?
Thank you very much
clarity vs precisionTuesday, July 26, 2011 8:12 AM
Answers
-
Hello,
Thank you for posting.
The state of the modified properties changes to Modified when the DetectChanges method was called. After the changes are saved, the object state changes to Unchanged.
Best Regards,
Larcolais Gong[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Giorgi Zautashvili Thursday, August 18, 2011 4:13 PM
Thursday, July 28, 2011 6:48 AM
All replies
-
You might want to have at look at Self-Tracking entities.
These are designed to track all changes (both primitive properties and relations) for a disconnected object graph.Regards,
KoenTuesday, July 26, 2011 8:16 AM -
Thanks Koen,
but I have to use POCOs, the project is designed this way already
Regards
clarity vs precisionTuesday, July 26, 2011 8:19 AM -
Ok,
in that case, you might want to have a look at this article:
http://msdn.microsoft.com/en-us/library/dd456829.aspx
Regards,
KoenTuesday, July 26, 2011 8:27 AM -
Thank you,I took a look,
but this didn't help either;
In my case my detached object is already "in a relationship" :) with other POCOs and when I "detect changes" the context doesn't recognize these complex changes and tries to update only the primitive ones, here's a code snippet:
public static int Edit(HRDepartment department) { //department is detached, coming from FormView update procedure //at this point it is in relationship with a parent department and this relationship needs to be persisted //HRDb.Instance gets a singleton (in a proper way:)) var context = ((IObjectContextAdapter)HRDb.Instance).ObjectContext; //here I attach department to the appropriate entity set context.AttachTo("HRDepartments", department); //I'm telling the state manager to treat department as an old but changed object context.ObjectStateManager.ChangeObjectState(department, EntityState.Modified); //this wasn't necessary, but ok :) context.DetectChanges(); //save changes context.SaveChanges(); return department.Id; }
Regards
clarity vs precisionTuesday, July 26, 2011 7:56 PM -
Hello,
Thank you for posting.
The state of the modified properties changes to Modified when the DetectChanges method was called. After the changes are saved, the object state changes to Unchanged.
Best Regards,
Larcolais Gong[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Giorgi Zautashvili Thursday, August 18, 2011 4:13 PM
Thursday, July 28, 2011 6:48 AM -
Hi,
as you can see in the snippet I'm not trying to change state after she changes are saved, why would I do so?
Ok, I'll try something else
Thank you
clarity vs precisionThursday, August 18, 2011 4:13 PM