This is a MVC with Identity 1.0 web application. I'm trying to create and assign roles to my users during the first migration. I want the roles to display on AspNetRoles and the references from Roles to UserIds to display on AspNetUserRoles.
I'm trying to use this template here: MVC 5 Seed Users and Roles
var store = new UserStore<ApplicationUser>(context); Gives me this error:
'MyModel.Models.ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'UserStore'. There is no implicit reference conversion from 'MyModel.Models.ApplicationUser' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.MyModel
var manager = new UserManager<ApplicationUser>(store); Gives me this error:
The type 'MyModel.Models.ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'UserManager'. There is no implicit reference conversion from 'MyModel.Models.ApplicationUser' to 'Microsoft.AspNet.Identity.IUser'.
manager.Create(user, "ChangeItAsap!");
manager.AddToRole(user.Id, "Admin");
These two lines gives me this error:
The name 'user' does not exist in the current context
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");
}