EF Core发生间歇性错误:连接不支持MultipleActiveResultSets
我有一个使用EF Core的ASP.Net Core应用程序。EF Core发生间歇性错误:连接不支持MultipleActiveResultSets
我使用ASP.Net标识并为我的应用程序的实体共享相同的DBContext。
我已将连接字符串设置为Azure SQL数据库以使MultipleActiveResultSet = True。
它适用于一两天,但最终还是失败,出现错误:
The connection does not support MultipleActiveResultSets.
我不认为火星是真正的问题,因为它的工作的头两天就涨。
我正在使用ASP.Net Core的内置DI来设置我的DbContext。
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(appDbContextConnectionString));
我的理解是,上面的DbContext的默认生命周期是瞬态的(每个Web请求)。
是否可以与ASP.Net Identity共享相同的DBContext,还是应该为应用程序的实体分别指向同一个DB?
我不知道这是EF Core,ASP.Net Core还是SQL Azure配置的问题。
我也一直在遇到这个问题,并且在连接字符串中设置MultipleActiveResultSet=True
没有太大的作用。
在Startup.cs
我的数据库配置非常相似,你有什么:
services.AddEntityFrameworkSqlServer()
.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
builder => builder.MigrationsAssembly("MyProject")),
ServiceLifetime.Transient
);
原来,我是越来越
The connection does not support MultipleActiveResultSets
,因为我有几个异步数据库查询访问同一实体。因此,一个数据库查询将检索一个实体,另一个将通过EF Core的新方法Include
包括相同的实体。另请参阅https://stackoverflow.com/a/46164203/4336725
我有几个异步数据库查询的原因是因为EF Core目前不支持延迟加载(与EF 6不同)。 https://github.com/aspnet/EntityFrameworkCore/issues/3797
我的解决方法是用不同的明确Include()
和ThenInclude()
定义几个IQueryable
秒。
https://github.com/aspnet/EntityFrameworkCore/issues/6491他是什么意思“中间件是否每个请求都有一个范围?” –
你可以分享你注入或创建你的数据库上下文的代码吗? – hugoterelle
您可能想要检查连接字符串是否在运行时实际使用了“MultipleActiveResultSet = True”。只是一个猜测。 https://stackoverflow.com/questions/12690453/sql-azure-getting-an-error-there-is-already-an-open-datareader-associated-wit –