locked
entity model update question RRS feed

  • Question

  • User632751675 posted

    hi,

    I'm using the data entity model.  When I update a table, I've been qualifying the fields that I want to update:  table.fieldname = value; etc.

     I saw an example that did not specify each field, but when I tried it, I got a message about there being another

    record in the database with the same key - so I couldn't add it.    Do you see something wrong with this ?  Do I need to spell out every field I want to update when I'm updating existing records ? 

    var upd = client.Find(id);

    client.attach(updatedclient);

    entry(updatedclient).State= system.Data.EntityState.Modified;

    SaveChanges();

    I also saw this :

    var original = db.Users.Find(updatedUser.UserId);
    
    if (original != null)
    {
        db.Entry(original).CurrentValues.SetValues(updatedUser);
        db.SaveChanges();
    }

    but I also get the same message about key already existing.

    hi,

    I figured out why I was unable to save without specifying each field - it seems to work.

    Thursday, July 23, 2015 2:27 PM

Answers

  • User2024324573 posted

    var upd = client.Find(id);

    client.attach(updatedclient);

    entry(updatedclient).State= system.Data.EntityState.Modified

    Try with

    var upd = client.Find(id);
    
    //Update new values here for upd object
    
    db.Entry(upd).State = EntityState.Modified;
    
    db.SaveChanges();
    

    Hope this will help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 3:13 PM
  • User-219423983 posted

    Hi maggiemays,

    I have found a similar thread that provides some methods about updating EF data and I think you could take a look.

    http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record

    Besides, the following considerations about the attach function you should know.

    •If the object being attached has related objects, those objects will also be attached to the object context.

    •Objects are added to the object context in an unchanged state.

    •The object that is passed to the Attach method must have a valid EntityKey value. If the object does not have a valid EntityKey value, use the AttachTo method to specify the name of the entity set.

    https://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.attach(v=vs.110).aspx

    I hope it’s useful to you.

    Best Regards,

    Weibo Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2015 4:47 AM

All replies

  • User2024324573 posted

    var upd = client.Find(id);

    client.attach(updatedclient);

    entry(updatedclient).State= system.Data.EntityState.Modified

    Try with

    var upd = client.Find(id);
    
    //Update new values here for upd object
    
    db.Entry(upd).State = EntityState.Modified;
    
    db.SaveChanges();
    

    Hope this will help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 3:13 PM
  • User-219423983 posted

    Hi maggiemays,

    I have found a similar thread that provides some methods about updating EF data and I think you could take a look.

    http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record

    Besides, the following considerations about the attach function you should know.

    •If the object being attached has related objects, those objects will also be attached to the object context.

    •Objects are added to the object context in an unchanged state.

    •The object that is passed to the Attach method must have a valid EntityKey value. If the object does not have a valid EntityKey value, use the AttachTo method to specify the name of the entity set.

    https://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.attach(v=vs.110).aspx

    I hope it’s useful to you.

    Best Regards,

    Weibo Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2015 4:47 AM