数据库操作预期影响 1 行,但实际影响 0 行实体框架核心



>我有以下代码

try
{
using (var context = await PrepareDatabase())
{
// Update temp roles by removing one row, this should affect that RemoveOldRoles below is removing rows from the Role-table
var entity = context.TempRawRoles.Where(x => x.Account == "brkar4").FirstOrDefault();
context.TempRawRoles.Remove(entity);
await context.SaveChangesAsync();
await CleanUp(context);
}
}
catch(Exception ex)
{
throw;
}

这是我的清理

private async Task CleanUp(SecurityContext context)
{
try
{
// Get only the global ids that belongs to our test accounts
List<Guid> ids = await GetCreatedTestGlobalIds(context);
List<IdentityEntity> identities = await context.Identities.Where(x => ids.Contains(x.GlobalId)).ToListAsync();
context.Identities.RemoveRange(identities);
await context.SaveChangesAsync(); // Throws error
}
catch(Exception ex)
{
throw;
}
}

在我的清理中执行await context.SaveChangesAsync();时,出现以下错误:

数据库操作预计影响 1 行,但实际影响 0 行 行。数据可能已被修改或删除,因为实体 加载。请参阅 http://go.microsoft.com/fwlink/?LinkId=527962 有关了解和处理开放式并发的信息 异常。

我想这与此有关:context.TempRawRoles.Remove(entity);

但是我该如何解决这个问题呢?

private async Task CleanUp(SecurityContext context)
{
try
{
// Get only the global ids that belongs to our test accounts
List<Guid> ids = await GetCreatedTestGlobalIds(context);
List<IdentityEntity> identities = await context.Identities.Where(x => ids.Contains(x.GlobalId)).ToListAsync();
await context.Identities.RemoveRangeAsync(identities); // modify here
await context.SaveChangesAsync(); 
}
catch(Exception ex)
{
throw;
}
}

最新更新