Entity Framework Reverts Changes
Solution 1:
If you're keeping a persistent EF database context in your worker role, it's possible you're seeing the effects of EF objects being cached.
Worker role loads an entity and does something with it. Since you're not creating and disposing the EF context each time, the entity stays cached.
User saves the entity and the database gets updated with their changes.
Worker role queries for the entity again, but since it's cached it returns the outdated, cached version. It does some sort of save operation, overwriting the user's edits with the cached values.
See Entity Framework and Connection Pooling, specifically,
When you use EF it by default loads each entity only once per context. The first query creates entity instace and stores it internally. Any subsequent query which requires entity with the same key returns this stored instance. If values in the data store changed you still receive the entity with values from the initial query.
Bottom line, you should never persist an EF database context for long periods of time. You may think of it as just an open database connection, but it is much more than that and "optimizing" things by keeping it around is a false savings and will cause bad things to happen. It's meant to be used in a UoW pattern where you create it, do what operations need to be done, and then dispose of it ASAP.
Post a Comment for "Entity Framework Reverts Changes"