Entity framework core 在Blazor+;CQRS体系结构

Entity framework core 在Blazor+;CQRS体系结构,entity-framework-core,cqrs,blazor-server-side,clean-architecture,Entity Framework Core,Cqrs,Blazor Server Side,Clean Architecture,在我的项目中,我面临着著名的“在前一个操作完成之前,在此上下文中开始了第二个操作”错误 我知道发生这种情况的原因,因为我在Razor页面上调用了相同的方法(GetPersons())来加载四个组合框。以下是我的基础架构/数据->上下文: public class PlaygroundBiRepository : IPlaygroundBiRepository { private readonly PlaygroundBiContext _context; public Pla

在我的项目中,我面临着著名的“在前一个操作完成之前,在此上下文中开始了第二个操作”错误

我知道发生这种情况的原因,因为我在Razor页面上调用了相同的方法(
GetPersons()
)来加载四个组合框。以下是我的基础架构/数据->上下文:

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
}
公共类PlaygroundBiRepository:IPlaygroundBiRepository
{
私人只读PlaygroundBiContext\u上下文;
公共PlaygroundBiRepository(PlaygroundBiContext上下文)=>\u上下文=上下文;
#地区人士
公共异步任务GetPersonByIdAsync(int id)=>wait\u context.Persons.FindAsync(id);
public IQueryable GetPersons()=>\u context.Persons;
公共异步任务GetPersonRoleByIdAsync(int id)=>Wait\u context.PersonRoles.FindAsync(id);
public IQueryable GetPersonRoles()=>\u context.PersonRoles;
公共异步任务GetRoleByIdAsync(int id)=>wait\u context.Roles.FindAsync(id);
public IQueryable GetRoles()=>\u context.Roles;
#端区
}
在阅读了这里和那里,我认为没有简单的解决办法。如果我想每次调用都使用一个新的DI,我将不得不在上下文中丢失DI

servicelifest.Transient
不起作用,调用链不是
async


请提供任何指针。

例外情况是,多个线程可能同时使用同一上下文实例或同一数据库连接。然而,这只是一个猜测


我有类似的问题,它是由
AddDbContextPool
更改为
AddDbContext
后引起的,它解决了您的猜测是正确的。然而,这不是我的解决办法。我已经在使用
AddDbContext
is存储库实例也是作用域生存期,所有使用wait的异步调用我没有提到显式作用域,问题在于
public IQueryable GetPersons()=>\u context.Persons不能写为异步的请查看