【发布时间】:2016-12-05 15:10:35
【问题描述】:
当我尝试使用用户和角色的种子数据更新数据库时,我收到“一个或多个实体的验证失败。有关详细信息,请参阅 'EntityValidationErrors' 属性。”错误。创建了角色,我可以在 db 表中看到它们,但是没有创建具有管理员角色的用户。角色也不会显示在创建和编辑用户视图中。
用于创建用户的 ViewModel:
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; }
}
用于编辑用户的 ViewModel:
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; }
}
我创建了自己的 ViewModel 来编辑和创建用户,因为场景是这样的,用户必须从管理员创建,所以为了保持分离,我创建了自己的 ViewModel,与 AccountViewModel 不同。但除此之外,还可以创建一个简单的用户,就像在 ASp.Net MVC 中开箱即用一样,使用个人用户帐户、电子邮件、用户名和密码。管理员有权添加更多详细信息,例如名字和姓氏并分配角色。
创建(和编辑)视图:
...
<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>
...
我为 ChechBoxListFor html 助手安装了 MvcCheckBoxList nuget。
创建用户的get action方法:
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);
}
还有Configuration类,种子方法:
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");
}
仅供参考,我将密码设置为至少需要 6 个字母,我评论了其他请求,例如RequiredLowercase、RequiredUppercase、RequireNonLetterOrDigit、RequireDigit。
那么谁能帮我解决这个问题,创建管理员用户并在编辑和创建视图中显示角色?
【问题讨论】:
标签: c# asp.net-mvc entity-framework asp.net-identity asp.net-roles