EF4 - 更新[表]设置@p = 0在哪里

时间:2011-06-03 20:16:17

标签: entity-framework entity-framework-4

在浏览SQL分析器时,我注意到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表的任何列。运行此查询会更新 RowVersion列(timestamp数据类型),这会在下一个UnitOfWork中导致OptimisticConcurrencyException。

快速的谷歌搜索让我看到了这个link,这证实了其他人也遇到了这种情况而没有找到解决方案。

非常感谢任何指针。

编辑:复制问题的示例代码。

enter image description here

用户和会话表具有外键关系。另外,在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();
        }
    }

我在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

下一个查询很奇怪。

    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

3 个答案:

答案 0 :(得分:4)

不确定这是否仍然存在问题,但这是MS http://support.microsoft.com/kb/2390624

的修补程序

答案 1 :(得分:3)

发现此链接在另一个论坛上引用,并且能够获取Kris Ivanov提到的修补程序的下载。

http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2390624

答案 2 :(得分:0)

不确定EF4,但是在4.1中我们通过将rowversion / timestamp设置为concurrenttoken = false来关闭rowversion / timestamp。

我们这样做是因为

  1. 这是我们的数据库中的计算字段
  2. 应用程序(在我们的例子中)永远不应该更改
相关问题