实体框架 4 (CTP 5) 并发性
自从我读到有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重点关注您的第一个比较,我相信默认情况下您在 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