gpt4 book ai didi

entity-framework - "Validation failed for one or more entities. See ' EntityValidationErrors 属性的解决方案以获取更多详细信息。”

转载 作者:行者123 更新时间:2023-12-04 08:31:33 27 4
gpt4 key购买 nike

我遇到了这个错误,它没有提供任何关于根本原因的详细信息,但我弄清楚了问题所在。我想分享它,以便其他遇到它的人能够成功解决问题。

我有以下类(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
}

如您所见,我使用附加属性扩展了 IdentityUser。在尝试创建新用户 UserManager.CreateAsync() 时,我的 View 只有 UserName 和 Password 作为表单字段。当尝试仅使用用户名和密码提交表单时,系统将尝试验证电子邮件是否已通过,因为它具有 Required 属性。

        //
// 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);
}

最佳答案

解决方案我必须将电子邮件作为字段添加到我的表单中,或者删除 Required 属性。一旦我完成其中任何一个,代码就会按预期工作。

希望对您有所帮助。

关于entity-framework - "Validation failed for one or more entities. See ' EntityValidationErrors 属性的解决方案以获取更多详细信息。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21486072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com