C# 如何查看实体框架的待定更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/592504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:31:53  来源:igfitidea点击:

How can I see the Entity Framework's pending changes?

c#.netentity-framework

提问by Zack Peterson

I'm creating an application with the ADO.NET Entity Framework.

我正在使用 ADO.NET Entity Framework 创建一个应用程序。

I can step through my code line-by-line while debugging and watch SQL Server Profiler for every query executed, but I can't figure out where all those SQL commands are coming from!

我可以在调试和观察 SQL Server Profiler 执行的每个查询的同时逐行执行我的代码,但我无法弄清楚所有这些 SQL 命令来自哪里!

Sometimes when I execute SaveChanges(), the Entity Framework performs unexpected, weird INSERTS. They sometimes break the application. I can't figure out what I'm doing to cause them.

有时,当我执行时SaveChanges(),实体框架会执行意外的、奇怪的插入。他们有时会破坏应用程序。我无法弄清楚我在做什么导致他们。

How can I monitor the pending changes that queue up waiting for a SaveChanges()call?

如何监控排队等待SaveChanges()呼叫的挂起更改?

回答by Richard

Take a look at

看一眼

myObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added)

here.

在这里

回答by maximpa

To monitor the events when entities are added to or removed from the state manager, you can use ObjectStateManagerChangedevent:

要在状态管理器中添加或删除实体时监视事件,您可以使用ObjectStateManagerChanged事件:

var ctx = new ModelContainer();

// ...

ctx.ObjectStateManager.ObjectStateManagerChanged += (sender, e) =>
{
   Trace.WriteLine(string.Format("{0}, {1}", e.Action, e.Element));
};

回答by kjbartel

Since Entity Framework 5.0 DbContexthas a ChangeTrackerpropertywhich has all pending changes. Similar to the ObjectStateManageryou can get entities in various states as follows:

由于 Entity Framework 5.0DbContext有一个ChangeTracker属性,其中包含所有待处理的更改。类似于ObjectStateManager您可以获取各种状态的实体,如下所示:

myDbContext.ChangeTracker.Entries().Where(e => e.State == EntityState.Added);
myDbContext.ChangeTracker.Entries().Where(e => e.State == EntityState.Deleted);
myDbContext.ChangeTracker.Entries().Where(e => e.State == EntityState.Modified);

回答by Ogglas

Entity Framework 6has a method for that, really useful.

Entity Framework 6有一个方法,非常有用。

dbContext.ChangeTracker.HasChanges()

Example:

例子:

if (dbContext.ChangeTracker.HasChanges())
{
    db.SaveChanges();
}

回答by speckledcarp

When I attempted to view the ObjectStateManagermentioned in the other answers it didn't show up in my debugger. When debugging in Visual Studio 2017, I've found it helpful to add the following snippet to a watch (which casts the dbContext object before referencing the ObjectStateManager property).

当我试图查看ObjectStateManager其他答案中提到的内容时,它没有出现在我的调试器中。在 Visual Studio 2017 中调试时,我发现将以下代码段添加到 watch(在引用 ObjectStateManager 属性之前转换 dbContext 对象)很有帮助。

((System.Data.Entity.Infrastructure.IObjectContextAdapter)this.db).ObjectContext.ObjectStateManager

((System.Data.Entity.Infrastructure.IObjectContextAdapter)this.db).ObjectContext.ObjectStateManager

Once the watch is made, you can debug line by line watching what actions add to the various properties that list pending changes.

监视完成后,您可以逐行调试,观察将哪些操作添加到列出未决更改的各种属性中。