views:

73

answers:

2

Hi, 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 = 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?

Thanks, Nir.

+2  A: 

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.

Shiraz Bhaiji
A: 

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

Alexander Taran