mur*_*uge 5 entity-framework entity-framework-4
在浏览SQL profiler时,我注意到EF4生成了以下查询.
exec sp_executesql N'declare @p int
update [dbo].[User]
set @p = 0
where (([UserID] = @0) and ([RowVersion] = @1))
select [RowVersion]
from [dbo].[User]
where @@ROWCOUNT > 0 and [UserID] = @0',N'@0 int,@1 binary(8)',@0=1,@1=0x000000000042DDCD
Run Code Online (Sandbox Code Playgroud)
我不确定为什么EF4会生成这个,而我实际上并没有更新UnitOfWork中User表的任何列.运行此查询会更新RowVersion列(timestamp数据类型),该列会在下一个UnitOfWork中导致OptimisticConcurrencyException.
一个快速的谷歌搜索引导我到这个链接,这确认其他人也遇到了这种情况,但尚未找到解决方案.
非常感谢任何指针.
编辑:复制问题的示例代码.

用户和会话表具有外键关系.此外,在EF4中,我已将两个实体的RowVersion列的"并发模式"属性设置为" 固定".
下面是复制方案的示例方法.
private static void UpdateSession()
{
using (var context = new TestEntities())
{
context.ContextOptions.ProxyCreationEnabled = false;
var session = context.Users.Include("Sessions").First().Sessions.First();
session.LastActivityTime = DateTime.Now;
context.ApplyCurrentValues("Sessions", session);
context.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
我从Sql profiler看到以下查询由EF4生成.
exec sp_executesql N'update [dbo].[Session]
set [LastActivityTime] = @0
where (([SessionID] = @1) and ([RowVersion] = @2))
select [RowVersion]
from [dbo].[Session]
where @@ROWCOUNT > 0 and [SessionID] = @1',N'@0 datetime2(7),@1 int,@2 binary(8)',@0='2011-06-20 09:43:30.6919628',@1=1,@2=0x00000000000007D7
Run Code Online (Sandbox Code Playgroud)
而下一个查询很奇怪.
exec sp_executesql N'declare @p int
update [dbo].[User]
set @p = 0
where (([UserID] = @0) and ([RowVersion] = @1))
select [RowVersion]
from [dbo].[User]
where @@ROWCOUNT > 0 and [UserID] = @0',N'@0 int,@1 binary(8)',@0=1,@1=0x00000000000007D3
Run Code Online (Sandbox Code Playgroud)