开发者

Entity Framework 4 (CTP 5) concurrency

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 = "C开发者_Go百科hange1"
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?


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜