Getting exception below when making Async calls.
Exception message:
A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
Stack trace:
at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.<EnterCriticalSectionAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.AspNetCore.Identity.RoleManager`1.<RoleExistsAsync>d__34.MoveNext()
if (!roleManager.RoleExistsAsync(role.Key).Result) { **Exception occurs here**
roleManager.CreateAsync(new ApplicationRole() { Name = role.Key, NormalizedName = role.Key.ToUpper(), System = true, Description = role.Value });
}
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0",
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore.Design": {
"type": "build",
"version": "1.1.0"
},
This is a common user error. No two sync or async operations can be in flight at the same time.
I have the same issue.
```C#
var ret = _context.AppProgramMenus.Where(p =>
p.Active == true &&
(p.Program.Title.Contains(searchString) || p.Menu.Title.Contains(searchString)))
.Select(p =>
new RolMenuProgram
{
Id = p.ProgramMenuId,
ProgramTitle = p.Program.Title,
MenuTitle = p.Menu.Title,
canCreate = GetCRUD(p.ProgramMenuId, roleid, "C"),
canRead = GetCRUD(p.ProgramMenuId, roleid, "R"),
canUpdate = GetCRUD(p.ProgramMenuId, roleid, "U"),
canDelete = GetCRUD(p.ProgramMenuId, roleid, "D")
});
```C#
private bool GetCRUD(int programMenuId, int? roleId, string crud)
{
var ret = false;
var rmp = _context.AppRoleMenuPrograms.Where(r => r.ProgramMenuId == programMenuId && r.RoleId == roleId).SingleOrDefault(); **** ERROR SHOWS HERE ****
switch (crud)
{
case "C":
ret = rmp != null ? rmp.AllowCreate : false;
break;
case "R":
ret = rmp != null ? rmp.AllowRead : false;
break;
case "U":
ret = rmp != null ? rmp.AllowUpdate : false;
break;
case "D":
ret = rmp != null ? rmp.AllowDelete : false;
break;
}
return ret;
}
@wgutierrezr It means you're trying to do two database operations at the same time. It's your job to make sure that doesn't happen. If you need to overlap database calls, you need two DbContexts.
Hi, Using two context I had the same issue.
@wgutierrezr Oh, I see your problem. While you're trying to load data from _context via _context.AppProgramMenus.Where(...).Select(...), at the same time you're trying to load data from _context by calling the GetCRUD method during the .Select(...) for the first data load.
You need to change your first query from
_context.AppProgramMenus.Where(...).Select(...) to _context.AppProgramMenus.Where(...).ToList().Select(...).
The .ToList() makes sure the first query is finished and all the results are in a list before it goes on to run the .Select(...) which calls GetCRUD and runs more queries.
@divega @anpete Should we try to make this work? It's not multiple threads, but rather one query that has calls that are (I assume) funcletized and themselves execute queries.
We need to investigate the consequences of doing this further to avoid issues like https://github.com/aspnet/EntityFramework/issues/8864
Having issue with this query
// preparing query i guess.
var parent = _service.NavigationStore.AsQueryable().Where( p => p.ParentId == null && p.ExtensionName.Equals( exn,StringComparison.OrdinalIgnoreCase ));
var child = _service.NavigationStore.AsQueryable().Where( p => p.ParentId > 0 && p.ExtensionName.Equals(exn, StringComparison.OrdinalIgnoreCase));
// execute to list i guess.
var data = parent.GroupJoin( child, p => p.Id, c => c.ParentId,
( p, g ) => new { Parent = p, Children = g } ).ToList();
says A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
Is this thread related? I am not using async task to do this.
Re-triage due to interaction with #9725
@AndriySvyryd Question from triage: is there a blocking reason this can't be done for 2.1? (We think it is related to "blocked" PR.)
Yes: https://github.com/aspnet/EntityFrameworkCore/issues/8864 and similar issues that could arise. @anpete can elaborate further
Note from triage: Since using the context inside client eval of the function is blocked by this issue, and this is in turn blocked by #8864, @anpete is going to investigate #8864 again to see whether we can do something for 2.1. However, #8864 looks quite hard, so we will cycle back again after the investigation and decide how to proceed.
Fixed in #17089
Thank you to every body, you help me alot.
Most helpful comment
@divega @anpete Should we try to make this work? It's not multiple threads, but rather one query that has calls that are (I assume) funcletized and themselves execute queries.