Tutorial :EntityFramework v4 ApplyCurrentValues not saving referenced property changes



Question:

I have a project where I am using EFv4 to save data back to my repository. This is working as expected for the simple properties on my object but does nothing for the related objects.

For example, I have a User object and a related property Roles that is a collection of Role entities.

If I update the User's lastActivity date and the Roles assigned to the user in my UI and then send the User object back to the repository to be updated I can see the new values in the User object for both lastActivity and the Roles, but when I call:

this.ObjectContext.ApplyCurrentValues(entitySet.Name.ToString(), entity);  this.ObjectContext.SaveChanges();  

only the simple properties are saved like the lastActivity date, the Role changes are ignored.

Do I need to do something special update all referenced objects?

UPDATE: I was searching the web a bit more and saw that someone mentioned that ApplyCurrentValues only affects the scalar properties. Still doesn't change my original question, but it could explain why the related entities are not being updated. Microsoft's documentation makes not mention of this from what I can see.


Solution:1

I entered this with MSFT and it appears to be working as expected. Here is the quote from MSFT:

ApplyCurrentValues does only work with the scalar properties on a single entity. What you are trying to accomplish would be easiest with self-tracking entities. Hopefully the switch to using these entities wouldn't be too painful because you can just swpa which code generation template you are using (from the default one to the self-tracking entity one). You will not have to change your model at all, and hopefully the changes to your repository would be small.

I have logged a documentation issue to clarify what ApplyCurrentValues does. Thank you for reporting this!

Here is the referenced question:


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »