I get this error from a method that is called from blazor server side component:
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.
Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at BlazorStore9.Business.Account.GetProfileAndRoles(Int32 profileID) in
from this method:
public async Task<EditBlogUserModel> GetProfileAndRoles(int profileID)
{
Profile profile = await context.Profiles.AsNoTracking().FirstOrDefaultAsync(p => p.Id == profileID);
if (profile == null)
return null;
//context.Entry(profile).State = EntityState.Detached;
AppUser user = null;
try
{
user = await userManager.FindByNameAsync(profile.UserName);
}
catch (Exception ex)
{ }
var roles = await roleManager.Roles.ToListAsync<IdentityRole>();
List<string> allRoles = roles.Where(r => r.Name != "Administrators" && r.Name != "Editors" && r.Name != "Users")
.Select(r => r.Name).ToList<string>();
IList<string> userRoles1 = await userManager.GetRolesAsync(user);
List<string> userRoles = userRoles1.Where(r => r != "Administrators" && r != "Editors" && r != "Users").ToList<string>();
List<RoleModel> rolesModel = new List<RoleModel>();
var keyValuePairs = configuration.GetSection("BlogRoles").GetChildren();
foreach (string role in allRoles)
{
if (userRoles.Find(r => r == role) != null)
{
rolesModel.Add(new RoleModel() { Text = keyValuePairs.FirstOrDefault(kvp => kvp.Key == role).Value, Value = role, Checked = true });
}
else
{
rolesModel.Add(new RoleModel() { Text = keyValuePairs.FirstOrDefault(kvp => kvp.Key == role).Value, Value = role, Checked = false });
}
}
return new EditBlogUserModel()
{
ProfileID = profileID,
Name = profile.UserName,
Email = profile.Email,
Roles = rolesModel
};
}
I tried to convert Tolist() to ToListAsync() and the whole method async, But I still get this error.
Caller component that its name is EditBlogUser.razor and is nested some levels inside other components:
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
Model = await account.GetProfileAndRoles(ProfileID);
StateHasChanged();
}
Startup file:
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(
Configuration["Connections:AppConnection:ConnectionString"]), ServiceLifetime.Transient);
services.AddDbContext<IdentityContext>(options =>
options.UseSqlServer(
Configuration["Connections:IdentityConnection:ConnectionString"]), ServiceLifetime.Transient);
How to get this to work?
GetProfileAndRolesand its caller? The code shown wouldn't cause the exception - Camilo Terevinto