.NET MediatR 和 EF Core DbContext 连接不支持 MultipleActiveResultSets
.NET MediatR and EF Core DbContext The connection does not support MultipleActiveResultSets
我正在使用 MediatR 处理我的请求,我需要从 EF Core (ApplicationDbContext) 注入 DbContext。我使用了一个界面来做到这一点。
第一个请求工作正常,但紧接着,第二个请求失败并显示此消息:
The connection does not support MultipleActiveResultSets.
我不想启用 MultipleActiveResultSets,我只是想弄清楚为什么会这样。
这是我的Startup.cs:
services.AddPersistence(Configuration);
services.AddInfrastructure(Configuration);
services.AddApplication();
在 AddPersistence 中,我注册了我的 ApplicationDbContext 并将 IApplicationDbContext 接口添加为 Scoped:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(dbConnectionString)
);
services.AddScoped<IApplicationDbContext>(provider => provider.GetService<ApplicationDbContext>());
这是我的 IApplicationDbContext 接口:
public interface IApplicationDbContext
{
DbSet<MyEntity> MyEntities { get; set; }
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}
我的ApplicationDbContext:
public sealed class ApplicationDbContext : DbContext, IApplicationDbContext
{
public DbSet<MyEntity> MyEntities{ get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
Database.EnsureCreated();
Database.Migrate();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
}
现在在 MediatR 处理程序中,我这样做:
private readonly IApplicationDbContext _applicationDbContext;
public FilterAircraftDelayCodesRequestHandler(IApplicationDbContext applicationDbContext)
{
_applicationDbContext = applicationDbContext;
}
我只想return MediatR 处理程序中的 ApplicationDbContext DbSet 中的对象列表:
var data = await _applicationDbContext.MyEntites.ToListAsync(cancellationToken: cancellationToken);
这里是我得到的异常:
System.InvalidOperationException: The connection does not support MultipleActiveResultSets.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 sou...
在这种情况下,问题出在 ApplicationDbContext
的构造函数中。
我使用了Database.MigrateAsync()
,产生了这个错误。
我以 non-async 的方式使用 Database.Migrate()
方法解决了这个异常。
我正在使用 MediatR 处理我的请求,我需要从 EF Core (ApplicationDbContext) 注入 DbContext。我使用了一个界面来做到这一点。
第一个请求工作正常,但紧接着,第二个请求失败并显示此消息:
The connection does not support MultipleActiveResultSets.
我不想启用 MultipleActiveResultSets,我只是想弄清楚为什么会这样。
这是我的Startup.cs:
services.AddPersistence(Configuration);
services.AddInfrastructure(Configuration);
services.AddApplication();
在 AddPersistence 中,我注册了我的 ApplicationDbContext 并将 IApplicationDbContext 接口添加为 Scoped:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(dbConnectionString)
);
services.AddScoped<IApplicationDbContext>(provider => provider.GetService<ApplicationDbContext>());
这是我的 IApplicationDbContext 接口:
public interface IApplicationDbContext
{
DbSet<MyEntity> MyEntities { get; set; }
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}
我的ApplicationDbContext:
public sealed class ApplicationDbContext : DbContext, IApplicationDbContext
{
public DbSet<MyEntity> MyEntities{ get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
Database.EnsureCreated();
Database.Migrate();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
}
现在在 MediatR 处理程序中,我这样做:
private readonly IApplicationDbContext _applicationDbContext;
public FilterAircraftDelayCodesRequestHandler(IApplicationDbContext applicationDbContext)
{
_applicationDbContext = applicationDbContext;
}
我只想return MediatR 处理程序中的 ApplicationDbContext DbSet 中的对象列表:
var data = await _applicationDbContext.MyEntites.ToListAsync(cancellationToken: cancellationToken);
这里是我得到的异常:
System.InvalidOperationException: The connection does not support MultipleActiveResultSets.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 sou...
在这种情况下,问题出在 ApplicationDbContext
的构造函数中。
我使用了Database.MigrateAsync()
,产生了这个错误。
我以 non-async 的方式使用 Database.Migrate()
方法解决了这个异常。