解决方案"验证失败的一个或多个实体。见'EntityValidationErrors“了解详情财产和QUOT。 [英] Solution for "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."
问题描述
我遇到这个错误,它没有提供任何详细的根本原因是什么,但我想通了什么问题。我想分享它,以便其他人遇到它可能在解决问题的成功。
我有以下类:
公共类BankUser:IdentityUser,IUserProfile
{
#区域IUserProfile成员
[显示(名称=名)]
公共字符串名字{获得;组; } [显示(名称=姓氏)]
公共字符串名字{获得;组; } [需要]
[显示(名字=电子邮件地址)
[EmailAddress的(的ErrorMessage =电子邮件地址无效)]
公共字符串电子邮件{获得;组; } [显示(NAME =时区)]
公众诠释的TimeZone {搞定;组; }
公共字典<字符串,字符串> TimeZoneOptions
{
得到
{
字典<字符串,字符串> D =新词典<字符串,字符串>();
d.Add((GMT -10:00)夏威夷,-10);
d.Add((GMT -9:00)阿拉斯加,-9);
d.Add((GMT -8:00)太平洋时间,-8);
d.Add((GMT -7:00)山地时间,-7);
d.Add((GMT -6:00)中部时间,-6);
d.Add((GMT -5:00)东部时间,-5);
d.Add(未知,0);
返回D组;
}
} [显示(NAME =手机)]
[电话(的ErrorMessage =无效的电话号码。)]
[StringLength(10,MinimumLength = 10的ErrorMessage =电话号码必须是10个字符长。)]
公共字符串电话{搞定;组; }
#endregion
}
正如你所看到的,我伸出IdentityUser具有附加属性。我的观点只是想创建一个新用户 UserManager.CreateAsync()
当有用户名和密码表单域。当试图只用用户名和密码提交表单系统将尝试验证,因为它有必需的属性,电子邮件传递。
//
// POST:/帐号/注册
[HttpPost]
[使用AllowAnonymous]
[ValidateAntiForgeryToken]
公共异步任务<&的ActionResult GT;寄存器(RegisterViewModel模型)
{
如果(ModelState.IsValid)
{
VAR用户=新BankUser(){用户名= model.UserName};
VAR的结果=等待UserManager.CreateAsync(用户,model.Password);
如果(result.Succeeded)
{
等待SignInAsync(用户,isPersistent:假);
返回RedirectToAction(指数,家);
}
其他
{
AddErrors(结果);
}
} //如果我们走到这一步,事情失败了,重新显示形式
返回查看(模型);
}
解决方案
我必须添加电子邮件作为我的表单字段或删除必需属性。有一次,我做了他们俩的code和预期一样。
希望有所帮助。
I encountered this error, it did not provide any detail as to what the root cause was but I figured out what the problem is. I wanted to share it so that others that encounter it might have success in solving the problem.
I had the following class:
public class BankUser : IdentityUser, IUserProfile
{
#region IUserProfile Members
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name = "Email Address")]
[EmailAddress(ErrorMessage="Invalid Email Address")]
public string Email { get; set; }
[Display(Name = "Time Zone")]
public int TimeZone { get; set; }
public Dictionary<string, string> TimeZoneOptions
{
get
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("(GMT -10:00) Hawaii", "-10");
d.Add("(GMT -9:00) Alaska", "-9");
d.Add("(GMT -8:00) Pacific Time", "-8");
d.Add("(GMT -7:00) Mountain Time", "-7");
d.Add("(GMT -6:00) Central Time", "-6");
d.Add("(GMT -5:00) Eastern Time", "-5");
d.Add("Unknown", "0");
return d;
}
}
[Display(Name = "Phone")]
[Phone(ErrorMessage = "Invalid phone number.")]
[StringLength(10, MinimumLength = 10, ErrorMessage = "Phone number must be 10 characters long.")]
public string Phone { get; set; }
#endregion
}
As you can see, I extended IdentityUser with additional properties. My view only had UserName and Password as form fields when trying to create a new user UserManager.CreateAsync()
. When trying to submit the form with just the UserName and Password the system would try to validate that Email was passed since it had the Required attribute.
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new BankUser() { UserName = model.UserName };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Solution I must either add the Email as a field in my form or remove the Required attribute. Once I did either of them the code worked as expected.
Hope that helps.
这篇关于解决方案&QUOT;验证失败的一个或多个实体。见'EntityValidationErrors“了解详情财产和QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!