这是一个带有Identity 1.0 Web应用程序的MVC。我在第一次迁移期间尝试创建并为我的用户分配角色。我希望角色显示在AspNetRoles上,以及从Roles到UserIds的引用显示在AspNetUserRoles上。
我试图在此处使用此模板:MVC 5 Seed Users and Roles
var store = new UserStore<ApplicationUser>(context);
给我这个错误:
&#39; MyModel.Models.ApplicationUser&#39;不能用作类型参数 &#39; TUSER&#39;在通用类型或方法&#39; UserStore&#39;。没有 来自&#39; MyModel.Models.ApplicationUser&#39;的隐式引用转换至 &#39; Microsoft.AspNet.Identity.EntityFramework.IdentityUser&#39; .MyModel
var manager = new UserManager<ApplicationUser>(store);
给我这个错误:
类型&#39; MyModel.Models.ApplicationUser&#39;不能用作类型 参数&#39; TUser&#39;在通用类型或方法&#39; UserManager&#39;。 没有隐式引用转换 &#39; MyModel.Models.ApplicationUser&#39;至 &#39; Microsoft.AspNet.Identity.IUser&#39;
manager.Create(user, "ChangeItAsap!");
manager.AddToRole(user.Id, "Admin");
这两行给了我这个错误:
名称&#39;用户&#39;在当前上下文中不存在
protected override void Seed(MyModel.Models.ApplicationDbContext context)
{
//Test Role
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
if (!context.Users.Any(u => u.UserName == "adminUser"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var passwordHash = new PasswordHasher();
string password = passwordHash.HashPassword("Password@123");
var users = new List<ApplicationUser>
{
new ApplicationUser { UserName = "adminUser", FirstMidName = "Admin", LastName = "Admin",Email="adminUser@gmail.com",PasswordHash = password,
EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1,DepotID = 1,IsAdministrator = true,SecurityStamp = Guid.NewGuid().ToString()},
new ApplicationUser { UserName = "User", FirstMidName = "User", LastName = "User",Email="user@gmail.com",PasswordHash = password,
EnrollmentDate = DateTime.Parse("2016-02-19"), DepartmentID = 2,DepotID = 2,IsAdministrator = false,SecurityStamp = Guid.NewGuid().ToString()},
};
users.ForEach(s => context.Users.AddOrUpdate(p => p.UserName, s));
context.SaveChanges();
manager.Create(user, "ChangeItAsap!");
manager.AddToRole(user.Id, "Admin");
}