我正在尝试将数据库提供程序从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,这就是为什么我必须更改提供程序。
更新:
该问题似乎仅在我尝试执行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();
}
}
连接字符串
服务器=本地主机:50000;数据库= db1; UID =用户; PWD =密码; CurrentSchema =待办事项;
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)
答案 0 :(得分:0)
SaveChangesAsync(),请改用 SaveChanges()。我知道这似乎很疯狂,可悲的事实。