我在项目中遇到了著名的“在上一次操作完成之前在此上下文中开始第二次操作”错误。
我知道发生这种情况的原因,因为我在剃刀页面上调用了相同的方法(GetPersons()
)以加载四个ComboBox。这是我的基础架构/数据->上下文:
public class PlaygroundBiRepository : IPlaygroundBiRepository
{
private readonly PlaygroundBiContext _context;
public PlaygroundBiRepository(PlaygroundBiContext context) => _context = context;
#region Person
public async Task<Person> GetPersonByIdAsync(int id) => await _context.Persons.FindAsync(id);
public IQueryable<Person> GetPersons() => _context.Persons;
public async Task<PersonRole> GetPersonRoleByIdAsync(int id) => await _context.PersonRoles.FindAsync(id);
public IQueryable<PersonRole> GetPersonRoles() => _context.PersonRoles;
public async Task<Role> GetRoleByIdAsync(int id) => await _context.Roles.FindAsync(id);
public IQueryable<Role> GetRoles() => _context.Roles;
# endregion
}
在这里到那里阅读后,我发现没有简单的解决方法。如果我想在每个呼叫中使用一个新的DI,我将不得不失去上下文中的DI。
ServiceLifetime.Transient
无效,并且呼叫链不是async
请使用任何指针。
例外,它看起来像同一上下文实例或同一DbConnection可能被多个线程同时使用。但是,这只是一个猜测。
我有类似的问题,这是由AddDbContextPool
更改为AddDbContext
后引起的