【发布时间】:2013-02-08 07:24:20
【问题描述】:
我们使用的是 VS 2012 附带的 Framework 4.5 和 EF 5.0。 我们有一个实体,其中包含一个名为 RowVersion 的列,我计划将其用于并发检查。它是 MySQL 表中的一个“时间戳”值,在插入或更新记录时会自行更新。
默认 EF 5 代码生成生成此 POCO:
Partial Public Class Terminal
Public Property ID As Long
Public Property User_ID As Nullable(Of Long)
Public Property Name As String
Public Property Number As Nullable(Of Integer)
Public Property Location As String
Public Property Description As String
Public Property IsEnabled As Boolean
Public Property RowVersion As Date
Public Overridable Property UserSession As ICollection(Of UserSession) = New HashSet(Of UserSession)
End Class
我已将“RowVersion”属性的并发模式设置为“fixed”以启用并发检查,“StoreGeneratedPattern”设置为“Computed”。 我们使用通过自动映射器映射的 DTO 通过 WCF 服务发送数据。
我们在更新通过服务返回的实体时遇到了一些问题。这是一些代码:
Public Sub SaveTerminal(Term As TerminalDto)
Using db As New PasgaEntities(conString)
Dim tmpTerminal As Terminal = Mapper.Map(Of Terminal)(Term)
db.Terminal.Attach(tmpTerminal)
db.Entry(tmpTerminal).State = EntityState.Modified
db.SaveChanges() 'throws OptimisticConcurrencyException
End Using
End Sub
即使“rowVersion”没有改变,这总是会引发 OptimisticConcurrencyException。 这是由于将 DTO 映射回 Entity 造成的吗?
谁能指出我正确的方向?
问候
【问题讨论】:
-
如何处理数据库中的行版本?它不应该是一个日期。
-
RowVersion 是一个时间戳字段,每次插入或修改数据时都会自动更新。 (dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html)
-
这是否意味着我必须在使用整数时手动增加 RowVersion?还是 EF 做的?
标签: entity-framework concurrency poco dto optimistic-concurrency