none
How to fix Error: Refresh ObjectStateManager entries in MVC 4 RRS feed

  • Question

  • I want to  insert into 2 table in 1 controller: example: 

    db.Entry(record).State = EntityState.Added;
    db.SaveChanges();
    //
    db.OtherTable.Add(newRecord);
    db.SaveChanges(); //Error here
    
    Then, i got this error: 

    Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries



    • Moved by George123345 Monday, March 17, 2014 5:36 AM not a Visio issue
    Saturday, March 15, 2014 3:09 PM

Answers

  • Hello,

    That's a side-effect of a feature called optimistic concurrency. The exception means that between time when fetching data from database and modified it, the data had been changed.

    By default, the Entity Framework implements an optimistic concurrency model. This means that locks are not held on data in the data source between when the data is queried and the data is updated.

    Are your project used by multiple users? If it is, then there will be a conflict, as we are using optimistic locking. Resolve concurrency issues using the ObjectStateManager entries, and Save changes again. Some sample code is there in MSDN site.

    And since you are using MVC, please check below link to see if you are under this scenario:

    http://stackoverflow.com/questions/1836173/entity-framework-store-update-insert-or-delete-statement-affected-an-unexpec

    It was caused by the entity's ID (key) field not being set.

    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.

    • Marked as answer by VMI TIM Sunday, March 23, 2014 6:02 PM
    Monday, March 17, 2014 6:44 AM
    Moderator

All replies

  • Hi,

    This is the forum to discuss questions and feedback for Microsoft Visio, I'll move your question to the MSDN forum for ADO.NET Entity Framework and LINQ to Entities

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework&filter=alltypes&sort=lastpostdesc

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    George Zhao
    TechNet Community Support

    Monday, March 17, 2014 5:35 AM
  • Hello,

    That's a side-effect of a feature called optimistic concurrency. The exception means that between time when fetching data from database and modified it, the data had been changed.

    By default, the Entity Framework implements an optimistic concurrency model. This means that locks are not held on data in the data source between when the data is queried and the data is updated.

    Are your project used by multiple users? If it is, then there will be a conflict, as we are using optimistic locking. Resolve concurrency issues using the ObjectStateManager entries, and Save changes again. Some sample code is there in MSDN site.

    And since you are using MVC, please check below link to see if you are under this scenario:

    http://stackoverflow.com/questions/1836173/entity-framework-store-update-insert-or-delete-statement-affected-an-unexpec

    It was caused by the entity's ID (key) field not being set.

    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.

    • Marked as answer by VMI TIM Sunday, March 23, 2014 6:02 PM
    Monday, March 17, 2014 6:44 AM
    Moderator
  • Thank for help. ..
    I have other way by execute sql command: db.Database.ExecuteSqlCommand(...); 
    Sunday, March 23, 2014 6:06 PM
  • Nice..thanks u !
    Sunday, March 23, 2014 6:07 PM