entity-framework - Entity Framework 更新失败

标签 entity-framework

我想更新 EF 中的一行:

Case newCase = new Case(...);
dbContext.Entry(newCase).State = EntityState.Modified;
dbContext.SaveChanges();

更新时我收到此消息:

Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded

没有并发性,因为我在我的机器上使用数据库。

如果我去 SQL Profiler 查看执行的查询,条件是:

WHERE [Id] = @p0 AND [RowVersion] = @p14;

在这种情况下,@p0为1,@p14为NULL,与我要修改的记录一模一样。但是,如果我在删除查询的 RowVersion 部分后在 SQL Server 中手动启动此查询,则会成功,更新 1 行。

问题可能是什么?我该如何解决?

编辑:

我尝试在更新之前选择记录,但错误是相同的:

    IEnumerable<Case> cases = from c in dbContext.Cases where c.Id.ToString() == "1" select c;
    Case cs = cases.SingleOrDefault();
    dbContext.Entry(cs).State = EntityState.Modified;
    dbContext.SaveChanges();

这是完整的错误:

An exception of type 'Microsoft.Data.Entity.DbUpdateConcurrencyException' occurred in EntityFramework.Core.dll but was not handled in user code

Additional information: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.

这是来自 Sql Profiler 的查询:

exec sp_executesql N'SET NOCOUNT OFF;

UPDATE [Case]
    SET 
        [Closed] = @p1
      , [Comment] = @p2
      , [ContactComment] = @p3
      , [ContactId] = @p4
      , [CreatedBy] = @p5
      , [CreatedDateTime] = @p6
      , [Description] = @p7
      , [Email] = @p8
      , [LastModifiedBy] = @p9
      , [LastModifiedDateTime] = @p10
      , [OpenedDateTime] = @p11
      , [Phone] = @p12
      , [RowVersion] = @p13
      , [SlaConsumedTime] = @p15
      , [SlaSuspended] = @p16
      , [SlaTotalTime] = @p17
      , [Status] = @p18
      , [Title] = @p19
WHERE 
    [Id] = @p0
    AND [RowVersion] = @p14;

SELECT @@ROWCOUNT;',

N'@p0 int,@p1 bit,@p2 nvarchar(max) ,@p3 nvarchar(max) ,@p4 int,@p5 int,@p6 datetime2(7),
@p7 nvarchar(4000),@p8 nvarchar(4000),@p9 int,@p10 datetime2(7),@p11 datetime2(7),
@p12 nvarchar(max) ,@p13 varbinary(max) ,@p14 varbinary(max) ,@p15 time(7),@p16 bit,
@p17 time(7),@p18 int,@p19 nvarchar(4000)',
@p0=1,@p1=0,@p2=NULL,@p3=NULL,@p4=0,@p5=1,@p6='2015-09-23 09:07:55.7041023',@p7=N'y',
@p8=N't@f.f',@p9=1,@p10='2015-09-23 09:50:02.9934006',@p11='2015-09-23 09:07:55.6796028',
@p12=NULL,@p13=NULL,@p14=NULL,@p15='00:00:00',@p16=0,@p17='00:00:00',
@p18=0,@p19=N'y'

最佳答案

这是因为,LinQ 的主键为 Null 值。

Case newCase = new Case(...);
dbContext.Entry(newCase).State = EntityState.Modified;
dbContext.SaveChanges();

检查您的对象,它是否传递主键Null。此异常通常仅是由于此原因造成的。

关于entity-framework - Entity Framework 更新失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32735610/

相关文章:

entity-framework - 从配置类编写 EF 迁移种子脚本

c# - Entity Framework 6 更新多对多而不更新或加载 Child

entity-framework - 提高 Entity Framework 中的批量插入性能

c# - NHibernate 3 或 4 等同于 Entity Framework Include

c# - Linq to Entities IncludeIf 扩展

entity-framework - EF4 - 数据未刷新/更新

asp.net - 为什么我必须在我的 UI 项目中引用 EF?

c# - 使用子生命周期时对象不会被释放

c# - 检索数据,奇怪的行为

c# - 违反了多重约束。关系 xxx 的角色 xxx 具有多重性 1 或 0..1