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

时间:2020-05-13 07:55:23

标签: c# .net-core entity-framework-core

我有以下代码

 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);

但是我该如何解决呢?

0 个答案:

没有答案