DBcontext is getting shared while being registered as Scoped

asp.net-core c# ef-core-2.2 entity-framework-6

Question

I am trying to migrate from ef6 to ef core and started getting below problems

System.InvalidOperationException: 'A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.'

on:

_dbContext
    .Query<EntityModel>()
    .FromSql(query, new SqlParameter("@userId", userId))
    .ToList();
1
0
4/19/2019 1:19:46 PM

Accepted Answer

I solved this once by adding MultipleActiveResultSets=true to the connectionstring.

Example:

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

From: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/enabling-multiple-active-result-sets

Just add it in your appsettings.json connectionstring

1
4/19/2019 1:29:04 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow