【发布时间】:2020-08-03 05:56:59
【问题描述】:
在我的项目中,我面临着著名的“在前一个操作完成之前在此上下文上启动了第二个操作”错误。
我知道它发生的原因,因为我在 Razor 页面上调用相同的方法 (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。
ServiceLifetime.Transient 不起作用,调用链不是async
请指点。
【问题讨论】:
标签: entity-framework-core cqrs blazor-server-side clean-architecture