While I try to update the database with the seed data for user and role I get a "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details." error. The roles get created, I can see them in the db table, but the user with the admin role doesn't get created. Also the roles are not being shown in the create and edit user view.
The ViewModel for creating the user:
public class InsertUser
{
[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
[Display(Name = "First Name")]
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "The {0} must start with upper letter and contain only alphabetical letters")]//requires the first character to be upper case and the remaining characters to be alphabetical
public string FirstName { get; set; }
[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
[Display(Name = "Last Name")]
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "The {0} must start with upper letter and contain only alphabetical letters")]
public string LastName { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Display(Name = "User Name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("Password", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public List<CheckBoxItem> Roles { get; set; }
}
public class CheckBoxItem
{
public string Id { get; set; }
public string Text { get; set; }
public bool Checked { get; set; }
}
The ViewModel for editing the user:
public class EditUser
{
public EditUser()
{
Roles = new List<CheckBoxItem>();
}
[Key]
public string Id { get; set; }
[Display(Name = "User Name")]
public string UserName { get; set; }
[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
[Display(Name = "First Name")]
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "The {0} must start with upper letter and contain only alphabetical letters")]
public string FirstName { get; set; }
[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
[Display(Name = "Last Name")]
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "The {0} must start with upper letter and contain only alphabetical letters")]
public string LastName { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
public List<CheckBoxItem> Roles { get; set; }
}
I created my own ViewModels for editing and creating the user cuz the scenario is like that, the users must be created from the admin, so to keep things separated I created my own ViewModel, different from AccountViewModel. But beside all of that a simple user can be created like it's out of the box in ASp.Net MVC with Individual User Account, with email, username and password. The admin has the privilege to add more details, like FirstName and LastName and assign the roles.
The create (and edit) view:
...
<div class="form-group">
@Html.LabelFor(model => model.Roles, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.CheckBoxListFor(model => model.Roles
, model => model.Roles
, model => model.Id
, model => model.Text
, model => model.Checked
, model => new { @class = "checkbox-inline" })
</div>
</div>
...
I installed the MvcCheckBoxList nuget for the ChechBoxListFor html helper.
the get action method for creating the user:
public ActionResult Create()
{
var items = new InsertUser();
items.Roles = _roleManager.Roles.Select(x => new CheckBoxItem { Id = x.Id, Text = x.Name, Checked = false }).ToList();
return View(items);
}
And the Configuration class, the seed method:
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.Roles.Any(r => r.Name == "user"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "user" };
manager.Create(role);
}
if (!context.Users.Any(u => u.UserName == "admin"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName = "admin", Email="admin@email.com" };
manager.Create(user, "password123");
manager.AddToRole(user.Id, "admin");
}
And just for information, I made the password to require minimum 6 letter, I commented the other requests like RequiredLowercase, RequieredUppercase, RequireNonLetterOrDigit, RequireDigit.
So can anybody help me how to fix this problem, to create the admin user and to display the roles in the edit and create views?
In the seed method, the if's where you create the roles, replace IdentityRole
with ApplicationRole