Intermittent error with EF Core: The connection does not support MultipleActiveResultSets

15,182

Solution 1

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

FetchStuffFromDBAsync();

Became:

await FetchStuffFromDBAsync();

And the problem was gone.

Solution 2

MultipleActiveResultSets=True need to add in connection string.

string connectionString = "Data Source=MSSQL1;Initial Catalog=AdventureWorks;Integrated Security=SSPI;MultipleActiveResultSets=True"; 

Solution 3

Multiple Active Result Sets (MARS) is a feature that works with SQL Server to allow the execution of multiple batches on a single connection. When MARS is enabled for use with SQL Server, each command object used adds a session to the connection.

The MARS feature is disabled by default.

You can enable it like below.

string connectionString = "Data Source=MSSQL1;" +   
    "Initial Catalog=AdventureWorks;Integrated Security=SSPI;" +  
    "MultipleActiveResultSets=True";  

Special Considerations When Using MARS

Share:
15,182

Related videos on Youtube

Jonas Arcangel
Author by

Jonas Arcangel

Updated on September 14, 2022

Comments

  • Jonas Arcangel
    Jonas Arcangel over 1 year

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

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

    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:

    The connection does not support MultipleActiveResultSets.

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

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

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

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

    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?

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

  • slasky
    slasky over 3 years
    Can you please explain why?
  • JvR
    JvR over 2 years
    Can you give more info on why it needs to be added though? Especially if it's intermittent and a new transient connection is created per request anyway