开发者

Entity Framework - Deleted association still appears in the second call

We have a long entity framework context running (don't ask why...), with a query which retrieves a user entity with its associations:

var user = entities.UserSet.Include("UserAddresses")
                   .Where(u => u.Id == 1).FirstOrDefault();

If a row of a user address is deleted from the database (by another process), and we run this query again, we still get the deleted row, even if we're setting the MergeOption before the call (in order to go the database in any case and not use the cache):

(Tried any set I have in the query, with no success)

entities.UserSet.MergeOption = 开发者_JAVA百科System.Data.Objects.MergeOption.OverwriteChanges;
entities.UserAddress.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
entities.UserSet.Include("UserAddresses").MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;

Can anyone help with this issue?


There is no option that will automatically delete an item in memory that is not on the disk.

http://blog.dynatrace.com/2009/03/11/adonet-entity-framework-unexpected-behaviour-with-mergeoptions/

You could clear the whole context, but that would probably cause you other problems.

Basically EF does not work well with long running contexts, it may be faster for you to fix the problem and get a more stable system by removing the long running contexts.


You got to .SaveChanges() before Selecting after Delete. No other way around this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜