在EntityFramework.dll中发生了类型为“System.Data.Entity.Validation.DbEntityValidationException”的首次机会异常。

4
我执行这段代码时收到了以下错误信息:
[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()
        {
        }
    }

关于异常的更多详细信息:

消息 "无法对处于添加状态的实体使用OriginalValues。" 字符串

堆栈跟踪:

StackTrace " 在 System.Data.Entity.Internal.InternalContext.SaveChanges()\r\n 在 System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\r\n
在 System.Data.Entity.DbContext.SaveChanges()\r\n 在 SoccerManager1.Models.DAL.UserRepository.SaveChanges() 中 d:\Stijn\Documenten\Visual Studio 2013\Projects\SoccerManager1\SoccerManager1\Models\DAL\UserRepository.cs:regel 48\r\n 在 SoccerManager1.Controllers.UserController.Registration(UserModel user) 中 d:\Stijn\Documenten\Visual Studio 2013\Projects\SoccerManager1\SoccerManager1\Controllers\UserController.cs:regel 72" 字符串

我只是想创建一个注册页面,我不明白为什么会收到这个错误。

我在这里做错了什么?如果我提供的信息不足,请告诉我。


可能是重复问题:https://dev59.com/imEh5IYBdhLWcg3wbi8W在-added-state中无法使用-originalvalues-for-entities - Daniel Casserly
1个回答

2

您的属性值可能存在验证失败。可能是密码,您将其设置为“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);
        }
    }
}

确实是密码问题,但是我发现它超过了20个字符的限制。我只是在跟随YouTube上的示例代码,哈哈,他没有遇到任何错误。不过这也无所谓,谢谢! - user4080876
@HardUser,很高兴能帮到你 :) - Mukesh Modhvadiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接