我正在尝试使用DevArt MySQL驱动程序以及DevArt MySQL Identity(8.21.2066.0)和EF 6 创建一个MVC5网络应用程序Asp.Net.Identity.Core(2.2.3)
在尝试将用户添加到角色时,我遇到了并发问题。(具体地说,这部分代码:var result1=UserManager.AddToRole(user.Id,"Admin");
跟踪堆栈如下:
System.Data.Entity.Infrastructure.DbUpdateConcurrencyException
HResult=0x80131501
Message=Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
Source=mscorlib
StackTrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<SaveChanges>d__61.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<UpdateAsync>d__40.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Identity.UserManager`2.<UpdateAsync>d__74.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Identity.UserManager`2.<AddToRoleAsync>d__105.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Identity.AsyncHelper.RunSync[TResult](Func`1 func)
at DEV_Art_Test_2.Startup.createRolesandUsers() in E:REPOSDevartNET Test 2DEV Art Test 2Startup.cs:line 50
at DEV_Art_Test_2.Startup.Configuration(IAppBuilder app) in E:REPOSDevartNET Test 2DEV Art Test 2Startup.cs:line 15
我正在运行以下我的启动程序来种子数据库:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
createRolesandUsers();
}
// In this method we will create default User roles and Admin user for login
private void createRolesandUsers()
{
ApplicationDbContext context = new ApplicationDbContext();
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
// In Startup iam creating first Admin Role and creating a default Admin User
if (!roleManager.RoleExists("Admin"))
{
// first we create Admin rool
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Admin";
roleManager.Create(role);
//Here we create a Admin super user who will maintain the website
var user = new ApplicationUser();
user.UserName = "bob";
user.Email = "test@test.com";
string userPWD = "Test1234";
var chkUser = UserManager.Create(user, userPWD);
//context.Entry(chkUser).State = System.Data.Entity.EntityState.Modified;
//context.SaveChanges();
//Add default User to Role Admin
if (chkUser.Succeeded)
{
var result1 = UserManager.AddToRole(user.Id, "Admin");
}
}
// creating Creating Manager role
if (!roleManager.RoleExists("Manager"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Manager";
roleManager.Create(role);
}
// creating Creating Employee role
if (!roleManager.RoleExists("Employee"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Employee";
roleManager.Create(role);
}
}
}
正如您在代码中看到的,我确实尝试将实体状态设置为modified,看看它是否会保存并得到相同的结果。对于我做错的事情,如有任何帮助,我们将不胜感激。
为什么不使用DependencyInjection来获得userManager
和roleManager
所需的服务。不需要ApplicationDbContext
。:
private void createRolesandUsers(app)
{
var userManager = app.ApplicationServices.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = app.ApplicationServices.GetRequiredService<RoleManager<IdentityRole>>();
// Creating default user and roles logic
}
将用户添加到角色中,是否使用用户Id或ApplicationUser对象?
await userManager.AddToRoleAsync(user, "Admin");