none
Merging Detached objects - Entity Code first 4.1 RRS feed

  • Question

  • Hi,

       I have an entity (User) which has say 10 fields. I am struggling for a way to edit the entity. I first load the entity and then pass it to client for updating, so it gets detached. Client is allowed to modify only 5 fields and these are the only ones that should be updated. Remaining 5 fields are readonly and come as null values from client. Now I want to merge the detached object (User) in such a way that only the 5 fields modified by client should be updated and the remaining 5 fields (which came as null from client) should remain the same (unchanged).

    Can any one please guide me, as to how I should cater the above scenario. 

    regards,

    Nirvan.

    Saturday, July 23, 2011 2:51 AM

Answers

  • Hi,

    The way you are doing this isn't there any easy way to do this other than doing it manually. You should send the same entity from the client to the server as you returned from the server to the client with all values not only the modified values, and then attach this to the dbset. Like this:

    context.User.Attach(user);
    context.Entry(user).State = EntityState.Modified;

    if you don't want to send all data back to the server, you need to append the modified data manually.


    --Rune
    • Marked as answer by Nirvan Sunday, July 24, 2011 2:24 PM
    Sunday, July 24, 2011 11:35 AM

All replies

  • Hi,

    The way you are doing this isn't there any easy way to do this other than doing it manually. You should send the same entity from the client to the server as you returned from the server to the client with all values not only the modified values, and then attach this to the dbset. Like this:

    context.User.Attach(user);
    context.Entry(user).State = EntityState.Modified;

    if you don't want to send all data back to the server, you need to append the modified data manually.


    --Rune
    • Marked as answer by Nirvan Sunday, July 24, 2011 2:24 PM
    Sunday, July 24, 2011 11:35 AM
  • Rune,

          I think what you are saying makes sense and I will stick to it. I will probably design the client to return all the fields of entity the to server. It seems that I was just unnecessarily trying to complicate matters !

    Thanks for your help.

    Nirvan. 

    Sunday, July 24, 2011 2:24 PM