【发布时间】:2021-06-23 23:27:18
【问题描述】:
我正在尝试将我的数据库提供程序从 SqlServer 切换到 Db2。 现在我面临的问题是,当我打电话时:
await SaveChangesAsync()
EF 丢弃错误
DbUpdateConcurrencyException:数据库操作预计会影响 1 行,但实际上影响了 0 行。自加载实体后,数据可能已被修改或删除。
同步操作正常。
await context.SaveChangesAsync(); // doesn't work
context.SaveChanges(); // works
最简单的方法是让它同步,但我也在使用 asp.net 核心身份,这完全是个问题。
我没有更多的想法如何解决这个问题,也许是错误。认为它可能像this,因为我之前使用过 EF Core 2.0,但在成功升级到 2.1.1 之后。错误仍然下降。
可能是提供商问题吗?使用最新版本的 IBM.Db2 Provider。
我公司常用的数据库是db2,所以我要换个provider。
更新:
该问题似乎仅在我尝试进行 INSERT 时才会发生。
在 Startup.cs 中
services.AddDbContext<MyDbContext>(options => {
options.UseLoggerFactory(Config.DbLogger);
options.UseDb2(Configuration.GetConnectionString("DB2Connection"), config =>
{
config.SetServerInfo(IBM.EntityFrameworkCore.Storage.Internal.IBMDBServerType.LUW, IBM.EntityFrameworkCore.Storage.Internal.IBMDBServerVersion.LUW_11_01_2020);
});
});
MyContext.cs
public class MyContext : DbContext
{
public DbSet<Event> Events { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Event>().ToTable("Events");
}
}
MyController.cs
public class MyController : Controller
{
private readonly MyContext context;
public MyController(MyContext _context)
{
context = _context;
}
[HttpPost]
public async Task<IActionResult> Create()
{
var _event = new Event { ID = Guid.NewGuid().ToString(), UserID = "adwadawdawdaw", TargetID = "1", Target = "todo", TimeStamp = DateTime.Now };
await context.Events.AddAsync(_event);
await context.SaveChangesAsync(); // <- Fails with "DbUpdateConcurrencyException"
// context.SaveChanges(); // <- works!
return Ok();
}
[HttpPut]
public async Task<IActionResult> Update()
{
var _event = context.Events.FirstOrDefault();
_event.TargetID = "2";
await context.SaveChangesAsync(); // <- works!
return Ok();
}
[HttpDelete]
public async Task<IActionResult> Delete()
{
var _event = context.Events.FirstOrDefault();
context.Events.Remove(_event);
await context.SaveChangesAsync(); // <- works
return Ok();
}
}
连接字符串
Server=localhost:50000;Database=db1;UID=user;PWD=password;CurrentSchema=todo;
DB2 版本 11.1
错误信息:
DbUpdateConcurrencyException: 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.
Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(int commandIndex, int expectedRowsAffected, int rowsAffected)
Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetWithoutPropagationAsync(int commandIndex, RelationalDataReader reader, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple<IEnumerable<ModificationCommandBatch>, IRelationalConnection> parameters, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList<InternalEntityEntry> entriesToSave, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken)
【问题讨论】:
-
好好看看这个问题。它是否有足够的代码来复制错误?不,你有任何意见吗?不,您是否显示任何预期的输出?否。您是否显示任何实际的错误消息?否 您又如何期待您的问题得到任何形式的回答?这就是为什么我对你的问题投反对票
-
如果同步方法有效而异步方法无效,很可能是一个错误。你最好检查/报告给他们的问题跟踪器。
-
@MortenBork,我说如果缺少某些输入,请告诉我。我知道这不足以找到问题所在。但问题的背景并不重要。事实是,如果它同步运行,它可以工作,如果不是,它就不起作用。带着我的问题,我希望有人遇到同样的问题并可能找到解决方法。这不是一个特定的问题。
-
dotConnect for DB2 2.3 支持可能适合您的 EF Core 2.1。
-
如果您有支持合同,请询问 IBM 是否是提供商问题。
标签: c# asp.net-core db2 entity-framework-core asp.net-core-identity