尝试从DbSet删除条目而不先加载它时,Remove上的DbUpdateConcurrencyException

时间:2019-03-11 17:45:59

标签: c# entity-framework dbset

我试图删除一个条目而不先从数据库中加载它。我尝试了无数种方法(所有这些方法我都可以在Internet上找到),但是似乎都不起作用:

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.

例如,此代码失败:

using (var dbContext = _dbContextCreator())
{
     var report = new DataAccess.AssetReport { Id = id };
     dbContext.AssetReports.Attach(report);
     dbContext.AssetReports.Remove(report);
     await dbContext.SaveChangesAsync();
}

AssetReport看起来像这样。

public class AssetReport
{
    [Key]
    public long Id { get; set; }
    public AssetReportType Type { get; set; }
    public AssetReportStatus Status { get; set; }
}

该条目存在于数据库中。其他请求不会失败。在相同的上下文中执行dbContext.AssetReports.Any将返回true。删除刚刚从数据库加载的记录也可以。

0 个答案:

没有答案