The data context of the object structure is not synchronized with the database?

So here is the situation -

  • I insert an item into the database calling AddtoObject () and then calling SaveChanges ().
  • Then I call the stored procedure to update the currently inserted record.
  • Then I call Save changes () again.
  • When requested, the database has the correct updated value, but the context entity structure has no updated values ​​... for the first time ... when I refresh the page, it gets the value ... but for the first time it never gets the updated values.

So has anyone encountered a similar problem at any time? What am I doing wrong here?

0
sync entity-framework-4 datacontext
source share
1 answer

The problem is that EF does not know what your stored procedure does, how to do this? This work is done on SQL Server. Therefore, after executing your stored procedure, you need to ask EF to update this (and other sibling) instance by issuing the Refresh() call:

 context.Refresh(RefreshMode.StoreWins, myObject); 

StoreWins tells the framework to overwrite the values ​​in the instance with the values ​​from the database.

+5
source share

All Articles