我的项目中遇到了著名的"在上一个操作完成之前,在此上下文上启动了第二个操作"错误。
我知道发生这种情况的原因,因为我在Razor页面上调用相同的方法(GetPersons()
(来加载四个ComboBoxes。这是我的基础设施/数据->上下文:
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
后,它解决了