none
How to return modified record

    Question

  • I have an entity model based on a sql database. When I query a record back after I updated it without calling db.SaveChanges(), I still get the old value. In my case, I want to get the last updated value so that I can continue updating it before I commit all the changes by calling db.SaveChanges(). How can I do that?

    MyTable myTable = (from item in db.MyTable where id == "record id" select item).FirstOrDefault();

    myTable.MyField = "New Value";

    myTable = (from item in db.MyTable where id == "record id" select item).FirstOrDefault();

    myTable.MyField still showing old value here, but I want to see the "New Value".


    Roy

    Sunday, November 10, 2013 2:15 PM

Answers

  • I am somewhat confused by what you are doing.  If you requery you will get the current contents of the database.  If you want the value you created in your code (the update) then why are you doing the requery?


    Lloyd Sheen

    Sunday, November 10, 2013 7:17 PM
  • Hello,

    As we know, the ‘db.SaveChanges()’ will commit all the changes we have done.

    From the codes, the myTable.MyField is actually not be saved into database. So when you call it again, of course it will fetch the old data.

    >>In my case, I want to get the last updated value so that I can continue updating it before I commit all the changes by calling db.SaveChanges(). How can I do that?

    Since you have already get the last record object, you can do the remained update operation with the object as long as you do not dispose it or set it to be null, why you want to query it again.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 11, 2013 2:50 AM

All replies

  • I am somewhat confused by what you are doing.  If you requery you will get the current contents of the database.  If you want the value you created in your code (the update) then why are you doing the requery?


    Lloyd Sheen

    Sunday, November 10, 2013 7:17 PM
  • Hello,

    As we know, the ‘db.SaveChanges()’ will commit all the changes we have done.

    From the codes, the myTable.MyField is actually not be saved into database. So when you call it again, of course it will fetch the old data.

    >>In my case, I want to get the last updated value so that I can continue updating it before I commit all the changes by calling db.SaveChanges(). How can I do that?

    Since you have already get the last record object, you can do the remained update operation with the object as long as you do not dispose it or set it to be null, why you want to query it again.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 11, 2013 2:50 AM