首页 / C# / c# – EntityFramework.dll中出现’System.Data.Entity.Validation.DbEntityValidationException’类型的第一次机会异常
c# – EntityFramework.dll中出现’System.Data.Entity.Validation.DbEntityValidationException’类型的第一次机会异常
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – EntityFramework.dll中出现’System.Data.Entity.Validation.DbEntityValidationException’类型的第一次机会异常,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2307字,纯文字阅读大概需要4分钟。
内容图文
![c# – EntityFramework.dll中出现’System.Data.Entity.Validation.DbEntityValidationException’类型的第一次机会异常](/upload/InfoBanner/zyjiaocheng/779/8ddb57f0b2e8446384bdba3f210b2cd4.jpg)
执行此代码时收到此错误:
[HttpPost]
public ActionResult Registration(UserModel user)
{
Console.WriteLine("ja");
try
{
if (ModelState.IsValid)
{
var crypto = new SimpleCrypto.PBKDF2();
var encrpPass = crypto.Compute(user.Password);
UserModel newUser = new UserModel(user.Email, encrpPass);
newUser.PasswordSalt = crypto.Salt;
userRepository.Add(newUser);
userRepository.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
Console.WriteLine(ex);
}
return View(user);
}
UserModel类:
public class UserModel
{
public int UserModelId { get; set; }
[Required]
[EmailAddress]
[StringLength(150)]
[Display(Name="Email address: ")]
public String Email { get; set; }
[Required]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 6)]
[Display(Name = "Password: ")]
public String Password { get; set; }
public String PasswordSalt { get; set; }
public UserModel(String email, String password)
{
this.Email = email;
this.Password = password;
}
public UserModel()
{
}
}
关于例外的更多细节:
Message “OriginalValues cannot be used for entities in the Added
state.” string
堆栈跟踪:
StackTrace ” bij
System.Data.Entity.Internal.InternalContext.SaveChanges()\r\n bij
System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\r\n
bij System.Data.Entity.DbContext.SaveChanges()\r\n bij
SoccerManager1.Models.DAL.UserRepository.SaveChanges() in
d:\Stijn\Documenten\Visual Studio
2013\Projects\SoccerManager1\SoccerManager1\Models\DAL\UserRepository.cs:regel
48\r\n bij
SoccerManager1.Controllers.UserController.Registration(UserModel user)
in d:\Stijn\Documenten\Visual Studio
2013\Projects\SoccerManager1\SoccerManager1\Controllers\UserController.cs:regel
72″ string
我只是想创建一个注册页面,我不明白我收到此错误的原因.
我在这做错了什么?如果这不是我提供的信息,请告诉我.
解决方法:
您的某个属性值的验证可能失败.可能是您将“StringLength”设置为20并且您正在插入加密密码的密码.或者可能没有为某些非空字段传递值.
为了调试和查找实际原因,您可以在catch中使用以下代码块:
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
foreach (var validationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Console.WriteLine("Property: {0} throws Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
内容总结
以上是互联网集市为您收集整理的c# – EntityFramework.dll中出现’System.Data.Entity.Validation.DbEntityValidationException’类型的第一次机会异常全部内容,希望文章能够帮你解决c# – EntityFramework.dll中出现’System.Data.Entity.Validation.DbEntityValidationException’类型的第一次机会异常所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。