开发者

How to view changes in EF-CodeFirst before they are committed?

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

If we break during SaveChanges开发者_JAVA百科 (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


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;
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜