为什么 EF 在 SaveChanges 上读取我未更改的对象?

发布于 2025-01-08 14:45:14 字数 1488 浏览 0 评论 0原文

作为 Entity Framework Code First 的一些测试的一部分,我正在测试更改跟踪。在一个小型测试数据库中,我的表中有两辆汽车,我对其运行测试方法:

Debug.WriteLine("Reading cars...");
var cars = context.Cars.ToArray();
Debug.WriteLine("Updating top speed of first car...");
Debug.WriteLine(string.Format("Type of car[0] is {0}", cars[0].GetType().ToString()));
cars[0].TopSpeed = 260;

Debug.WriteLine("Saving changes...");
context.SaveChanges();

我已将跟踪输出添加到 TopSpeedBrand 的 getter 和 setter Car 类的属性以查看如何访问它们。 TopSpeed 是一个 int?BrandBrand 实体的导航属性。运行上面的代码会得到下面的输出。

Reading cars...
Setting TopSpeed to 210 for ABC123.
Car: Getting TopSpeed for ABC123.
Setting TopSpeed to 250 for XYZ987.
Car: Getting TopSpeed for XYZ987.
Updating top speed of the first car...
Type of car[0] is System.Data.Entity.DynamicProxies.Car_18E3E11297DC48759312BDF1C2FFEBE9F19BAE5D487CED2A9781A6CA730071EA
Setting TopSpeed to 260 for ABC123.
Saving changes...
Car: Getting Brand for ABC123.
Car: Getting Brand for XYZ987.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for XYZ987.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.

该对象的类型是用于更改跟踪的 EF 动态代理。尽管如此,当调用 SaveChanges() 时,会读取未更改的 XYZ987 汽车的属性。我认为更改跟踪会导致 EF 仅读取已知会更改的对象,还是我遗漏了某些内容?我还需要添加其他内容才能启用更改跟踪吗?

As part of some tests of Entity Framework Code First I'm testing the change tracking. In a small test database I have two cars in a table that I run a test method against:

Debug.WriteLine("Reading cars...");
var cars = context.Cars.ToArray();
Debug.WriteLine("Updating top speed of first car...");
Debug.WriteLine(string.Format("Type of car[0] is {0}", cars[0].GetType().ToString()));
cars[0].TopSpeed = 260;

Debug.WriteLine("Saving changes...");
context.SaveChanges();

I've added trace output to the getters and setters of the TopSpeed and Brand properties of the Car class to see how they are accessed. TopSpeed is an int? and Brand is a navigation property to the Brand entity. Running the above code gives the output below.

Reading cars...
Setting TopSpeed to 210 for ABC123.
Car: Getting TopSpeed for ABC123.
Setting TopSpeed to 250 for XYZ987.
Car: Getting TopSpeed for XYZ987.
Updating top speed of the first car...
Type of car[0] is System.Data.Entity.DynamicProxies.Car_18E3E11297DC48759312BDF1C2FFEBE9F19BAE5D487CED2A9781A6CA730071EA
Setting TopSpeed to 260 for ABC123.
Saving changes...
Car: Getting Brand for ABC123.
Car: Getting Brand for XYZ987.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for XYZ987.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.
Car: Getting TopSpeed for ABC123.

The type of the object is an EF dynamic proxy for change tracking. Still, when calling SaveChanges() the properties of the unchanged XYZ987 car are read. I thought that change tracking would cause EF to only read the objects that were known to change, or am I missing something? Is there something else I need to add to enable change tracking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

櫻之舞 2025-01-15 14:45:14

即使满足了更改跟踪代理的所有要求,EF 仍然使用自动更改跟踪,除非明确关闭:

context.Configuration.AutoDetectChangesEnabled = false;

AutoDetectChangesEnabled 上的文档令人困惑:

如果自动检测配置更改则为 true
已启用;否则为假。

查看 ADO.NET 的 博客 他们解释得正确。

Even though all requirements for a change tracking proxy was fulfilled, EF still uses the automatic change tracking unless explicitly turned off:

context.Configuration.AutoDetectChangesEnabled = false;

The documentation on AutoDetectChangesEnabled is confusing:

True if the automatic detection of changes in the configuration is
enabled; otherwise, false.

Looking at the ADO.NET's blog they explain it correctly.

玩心态 2025-01-15 14:45:14

在 .NET 4 上运行的 EF 4.1、4.2 和 4.3 中存在一个错误(或者更确切地说,缺乏优化),这会导致 DetectChanges 处理应该知道不需要更改跟踪的实体。这是因为在 .NET 4 上运行的 EF 不会跟踪是否有任何实体需要更改跟踪,因此它必须每次都进行检查。

当 EF5 在 .NET 4.5 上运行时,此问题已在 EF5 上得到修复,该版本很快就会推出测试版。我刚刚验证,使用 .NET 4.5 上的 EF5,即使 AutoDetectChangesEnabled 设置为 true,SaveChanges 中也不会调用未更改更改跟踪代理的属性的 getter。

话虽这么说,即使在 EF 4.1-4.3 上,如果 EF 所做的所有事情都是检查实体是否是更改跟踪代理,我也不认为应该调用 getter,因此这里似乎可能存在另一个错误。

There is a bug (or rather, lack of an optimization) in EF 4.1, 4.2, and 4.3 running on .NET 4 which causes DetectChanges to process entities that should be known to not need change tracking. This is because EF running on .NET 4 does not keep track of whether or not there are any entities that need change tracking or not, so it has to check each time.

This is fixed on EF5 when running on .NET 4.5, which will be available in beta very soon now. I just verified that with EF5 on .NET 4.5 the getters for properties of unchanged change-tracking proxies are not called in SaveChanges, even with AutoDetectChangesEnabled set to true.

That being said, even on EF 4.1-4.3 I don't believe that the getter should be called if all EF is doing is checking whether or not the entity is a change tracking proxy, so it seems like there might be another bug here.

抱猫软卧 2025-01-15 14:45:14

要启用动态更改跟踪,您需要将所有标量(非导航)属性设为虚拟。

To enable dynamic change tracking you need make all your scalar(non navigational) properties virtual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文