[英](EFCore & Identity) A second operation started on this context before a previous operation completed
I get this error from a method that is called from blazor server side component:我从 blazor 服务器端组件调用的方法中收到此错误:
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.我试图将 Tolist() 转换为 ToListAsync() 和整个方法异步,但我仍然收到此错误。
Caller component that its name is EditBlogUser.razor and is nested some levels inside other components:调用者组件,其名称为 EditBlogUser.razor 并嵌套在其他组件中的某些级别:
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?如何让这个工作?
In blazor component that Lists the User Profiles, I put each component in a Bootstrap Popup, But I call the GetProfileAndRoles method from component's OnInitializedAsync lifecycle method, This caused all popup components to initialize when parent component loads, So I moved the Initialization code that calls GetProfileAndRoles method to popup's Click event, Now the component initializes only with a click event.在列出用户配置文件的 blazor 组件中,我将每个组件放在 Bootstrap Popup 中,但是我从组件的 OnInitializedAsync 生命周期方法中调用 GetProfileAndRoles 方法,这导致所有弹出组件在父组件加载时初始化,所以我移动了调用的初始化代码GetProfileAndRoles 方法来弹出的 Click 事件,现在组件只用一个 click 事件初始化。 And it is working.它正在发挥作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.