实体框架 4 (CTP 5) 并发性

发布于 2024-10-11 14:05:42 字数 876 浏览 2 评论 0原文

自从我读到有关 Code-First 的内容后,我发现这可能存在问题(尽管它只是预览版),我对 EF4 CTP5 版本有 2 个问题,如下所示:

正如它所说的“新更改跟踪 API” 但它不跟踪我的变化 猜测。与 LINQ to SQL 相比 举个例子看看是什么 每种方法的反应:

LINQ to SQL:

Dim db2 As New LINQDataContext
Dim db3 As New LINQDataContext

db2.Product.FirstOrDefault(Function(m) m.ID = 100).Name = "Change1"
db3.Product.FirstOrDefault(Function(m) m.ID = 100).Name = "Change2"

db2.SubmitChanges()
db3.SubmitChanges()

EF 4 CTP5:

Dim db2 As New ProductContext
Dim db3 As New ProductContext

db2.Product.FirstOrDefault(Function(m) m.ID = 100).Name = "Change1"
db3.Product.FirstOrDefault(Function(m) m.ID = 100).Name = "Change2"

db2.SaveChanges()
db3.SaveChanges()  

这些代码仅在上下文中有所不同。在 LINQ to SQL 中,第二个 SubmitChanges 将引发“未找到或更改行。”异常,但在 EF 中它将忽略更改并继续更改行两次,我认为这很糟糕,因为我们必须考虑并发性,对吗?

ever since I read about Code-First I find out there might be a problem with this (although its just a preview) I have 2 problem with EF4 CTP5 release as folowing:

As it said "New Change Tracking API"
but it dose't tracking changes i
guess. In comparison with LINQ to SQL I
bring an example to see what
each method react:

LINQ to SQL:

Dim db2 As New LINQDataContext
Dim db3 As New LINQDataContext

db2.Product.FirstOrDefault(Function(m) m.ID = 100).Name = "Change1"
db3.Product.FirstOrDefault(Function(m) m.ID = 100).Name = "Change2"

db2.SubmitChanges()
db3.SubmitChanges()

EF 4 CTP5:

Dim db2 As New ProductContext
Dim db3 As New ProductContext

db2.Product.FirstOrDefault(Function(m) m.ID = 100).Name = "Change1"
db3.Product.FirstOrDefault(Function(m) m.ID = 100).Name = "Change2"

db2.SaveChanges()
db3.SaveChanges()  

These codes are only different in their Contexts. In LINQ to SQL, the second SubmitChanges will rise an exception of "Row not found or changed.", but in EF it will ignore the changes and continue changing the row twice, which I think is bad because we must consider concurrency, right?

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

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

发布评论

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

评论(1

固执像三岁 2024-10-18 14:05:42

重点关注您的第一个比较,我相信默认情况下您在 LINQ to SQL 中进行了一些乐观并发检查。在 EF 中,您必须显式标记要检查并发性的任何属性。由于您很可能没有对 Name 属性执行此操作,因此 EF 并不关心有人在另一个数据库命令中更改了 Product 名称。

我还想指出,这并不是特定的代码优先行为,而是整个 EF 的行为。如果您创建实体数据模型,您还必须明确标记并发检查的属性。

如果您使用的是 Fluent API,请查找 IsConcurrencyToken 作为要在属性上设置的属性。如果您使用注释,请查看 ConcurrencyCheckAttribute。


朱莉

Focusing on your first comparison, I believe that you have some optimistic concurrency checking on by default in LINQ to SQL. IN EF, you have to explicitly mark any properties that you want to be checked for concurrency. Since you most likely have not done that for the Name property, EF doesn't care that someone changed the Product name in another database command.

I also want to point out that this isn't specifcally a code first behavior but it is a behaviour throughout EF. If you create an Entity Data Model, you also have to expclitily mark properties for concurrency checking.

If you are using the fluent API, look for IsConcurrencyToken as an attribute to set on a property. If you are using annotation, take a look at ConcurrencyCheckAttribute.

hth
julie

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