本文介绍了EF Core间歇性错误:连接不支持MultipleActiveResultSets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用EF Core的ASP.Net Core应用程序.

I have an ASP.Net Core application that uses EF Core.

我使用ASP.Net Identity并为我的应用程序实体共享相同的DBContext.

I use ASP.Net Identity and share the same DBContext for my app's entities.

我已将连接字符串设置为Azure SQL数据库以具有MultipleActiveResultSet = True.

I have set my connection string to an Azure SQL database to have MultipleActiveResultSet=True.

它可以工作一两天,但最终会失败并显示以下错误:

It works for a day or two, but eventually it fails with the error:

我认为MARS并不是真正的问题,因为它在开始运行的前两天就起作用了.

I don't think MARS is the real issue since it worked for the first two days it was up.

我正在使用ASP.Net Core的内置DI来设置我的DbContext.

I am using ASP.Net Core's built-in DI to set my DbContext.

services.AddDbContext<AppDbContext>(options =>
  options.UseSqlServer(appDbContextConnectionString));

我的理解是,上面的DbContext的默认生存期是瞬态"(每个Web请求).

My understanding is that the default lifetime for the DbContext above is Transient (per web request).

是否可以与ASP.Net Identity共享同一个DBContext,还是应该为我的应用程序实体指定一个单独的DBContext,指向同一个DB?

Is it alright to share the same DBContext with ASP.Net Identity or should I have a separate one for my app's entities, pointed to the same DB?

我不知道这是否与EF Core,ASP.Net Core或SQL Azure配置有关.

I don't know if this is an issue with EF Core, ASP.Net Core or with SQL Azure configuration.

推荐答案

我遇到了类似的问题,发现问题是我错过了方法前的await语句.

I had a similar issue and I found that the problem was that I missed an await statement in front of my method.

FetchStuffFromDBAsync();

蜜饯:

await FetchStuffFromDBAsync();

问题消失了.

这篇关于EF Core间歇性错误:连接不支持MultipleActiveResultSets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 18:15