EF4 - update [Table] set @p = 0其中 [英] EF4 - update [Table] set @p = 0 where
问题描述
通过SQL分析器,我注意到EF4生成的以下查询。
While going through SQL profiler, I noticed the following query generated by 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
我不知道为什么EF4生成这个,而实际上我没有更新该UnitOfWork中的User表的任何列。运行此查询将更新在下一个UnitOfWork中导致OptimisticConcurrencyException的 RowVersion列(timestamp数据类型)。
I am not sure why EF4 generates this while I am actually not updating any columns of the User table in that UnitOfWork. Running this query updates the RowVersion column (timestamp datatype) which leads to OptimisticConcurrencyException in the next UnitOfWork.
一个快速的搜索引导我进入这个< a href =http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/e2444f09-d6e1-4fdc-84d3-aaa28dc98fd8/ =nofollow noreferrer>链接,这证实其他人也遇到这种情况,而没有找到解决方案。
A quick googling led me to this link, which confirms that others have also run into this scenario without finding a solution yet.
将非常感谢任何指针。
编辑:这个问题。
用户和会话表具有外键关系。另外,在EF4中,我将两个实体的RowVersion列的并发模式属性设置为固定。
User and Session tables have a foreign key relationship. Also, in EF4 I have set the "Concurrency Mode" property of RowVersion columns of both entities to Fixed.
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();
}
}
我从Sql profiler看到以下查询被EF4。
I see from Sql profiler the following queries being genrated by 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
下一个查询是奇怪的。
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
推荐答案
不知道这是否仍然是您的问题,但这里是MS修复程序 http://support.microsoft.com/kb/2390624
Not sure if this is still problem for you but here is the hotfix by MS http://support.microsoft.com/kb/2390624
这篇关于EF4 - update [Table] set @p = 0其中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!