Entity framework data context not in sync with database?
So, here is the situation -
- I insert an item in the database calling the AddtoObject() and then call SaveChanges().
- Then, I call a stored procedure to update the currently inserted record.
- Then, I call the Save changes() again.
- The database when I query it has the correct updated value, but the entity framework context does not have the updated values..the first time..whenever I refresh the page it gets the value..but the first time it never gets the updated values.
So has anyone fac开发者_JS百科ed a similar issue anytime ? What am I doing wrong here ?
The problem is that the EF does not know what your stored procedure is doing, how could it? That work is done at the SQL Server. So after your stored procedure executes, you need to ask EF to update that (and other related) instance by issuing a Refresh()
call:
context.Refresh(RefreshMode.StoreWins, myObject);
The StoreWins
tells the framework to overwrite values in the instance with values from the database.
精彩评论