locked
db.Entry not working RRS feed

  • Question

  • User-515183 posted

    I have tried to use the db.Entry(data) where data is

    var data = db.table.Find(id);
    context.Entry(data).State = EntityState.Modified;

    db.SaveChanges();

    Not working does not update the table in the database.  I would like to do this versus lising over 40 something columns.

    Thank you!

    Saturday, June 17, 2017 7:32 PM

Answers

  • User2053451246 posted

    You are just loading the record from the database and calling save.  Where are you changing any property values?

    You would need to post the modified entity from the page to the controller, and then call save.

    NOTE: Make sure your page has every field in the entity or else the ones not included will be null.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 19, 2017 1:28 PM
  • User-515183 posted

    I used automapper to accomplish worked great! thank you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 19, 2017 10:56 PM

All replies

  • User1777532290 posted

    Hi,

    You have to change the state of the entity before saving it. In your code, it seems like you didn't change anything so there is no dirty state to be saved. 

    for example.

    var data = db.table.Find(id);

    data.Name = "<name>"

    context.Entry(data).State = EntityState.Modified;

    db.savechanges
    Sunday, June 18, 2017 12:33 AM
  • User-515183 posted
    I have over 40 columns where the user could change any value I thought doing the db.entry was a way to save any
    Value changed without listing all the fields like database.name = nh.name??
    Sunday, June 18, 2017 12:45 AM
  • User-1838255255 posted

    Hi sbuggle,

    According to your description, as far as I know, if you want to update database, you need check the following steps:

    1. Get student from DB.

    2. Change student name in disconnected mode (out of ctx scope).

    3. Mark entity as modified.

    4. Call SaveChanges.

    For more details, please check the following official document:

    Update Existing Entity using DBContext in Disconnected Scenario:

    http://www.entityframeworktutorial.net/EntityFramework4.3/update-entity-using-dbcontext.aspx 

    Best Regards,

    Eric Du

    Monday, June 19, 2017 11:18 AM
  • User2053451246 posted

    You are just loading the record from the database and calling save.  Where are you changing any property values?

    You would need to post the modified entity from the page to the controller, and then call save.

    NOTE: Make sure your page has every field in the entity or else the ones not included will be null.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 19, 2017 1:28 PM
  • User-515183 posted

    I used automapper to accomplish worked great! thank you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 19, 2017 10:56 PM