Question

I am trying to snoop on changes which are being persisted in EF-Code First before they are committed.

If we break during SaveChanges (as below) and drill down the watch (below below) you get to the non-public collection ObjectStateManager._deletedEntityStore which lists the persited items which are to be deleted.

This would be ideal for what I need but it is non-public. Does anyone know of any other way to get at this information?

(this.Units.Local is not sufficient.)

public class MyDbContext: DbContext
{   
    public DbSet<Unit> Units { get; set; }  
    public override int SaveChanges()
    {
        // Break here...
    }
}

this.ChangeTracker._internalContext.ObjectContext.ObjectStateManager._deletedEntityStore

Cheers, T

Was it helpful?

Solution

You can get to the underlying ObjectContext by casting your DbContext as IObjectContextAdapter. From there you should be able to hook up to the ObjectStateManager.

In fact, if the underlying ObjectContext is really important you can expose it as a public property. Like so:

public ObjectContext UnderlyingContext
{ 
    get
    {
        return ((IObjectContextAdapter)this).ObjectContext;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top