EntityFramework.dll中出现了“System.Data.Entity.Validation.DbEntityValidationException”类型的第一次机会异常 [英] A first chance exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll

查看:756
本文介绍了EntityFramework.dll中出现了“System.Data.Entity.Validation.DbEntityValidationException”类型的第一次机会异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [HttpPost] 
public ActionResult注册(UserModel用户)
{
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();

返回RedirectToAction(Index,Home);
}
}
catch(System.Data.Entity.Validation.DbEntityValidationException ex)
{

Console.WriteLine(ex);
}


return View(user);
}

UserModel类:

  public class UserModel 
{
public int UserModelId {get;组;
[必需]
[EmailAddress]
[StringLength(150)]
[显示(名称=电子邮件地址:)]
public String Email {get ;组;
[必需]
[DataType(DataType.Password)]
[StringLength(20,MinimumLength = 6)]
[显示(名称=密码:)]
public String Password {get;组; }
public String PasswordSalt {get;组;

public UserModel(String email,String password)
{
this.Email = email;
this.Password = password;
}

public UserModel()
{
}
}

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


消息OriginalValues不能用于添加的实体
状态。字符串


StackTrace:


StackTrace bij
System.Data.Entity.Internal.InternalContext.SaveChanges()\r\\\
bij
System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\r\\\


bij System.Data.Entity.DbContext.SaveChanges()\r\\\
bij

d中的SoccerManager1.Models.DAL.UserRepository.SaveChanges():\Stijn\ Documenten \Visual Studio
2013\Projects\SoccerManager1\SoccerManager1\Models\DAL\UserRepository.cs:regel
48\r\\\
bij
SoccerManager1。控制器.UserController.Registration(UserModel用户)
在d:\Stijn\Documenten\Visual Studio
2013\Projects\SoccerManager1\SoccerManager1\Controllers\UserController.cs:regel
72string


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



我在这里做错什么?如果这不够,我提供的信息请让我知道。

解决方案

您的属性值之一的验证可能失败。可能是您将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 );
}
}
}


I receive this error when executing this code:

[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 class:

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()
        {
        }
    }

More details on the exception:

Message "OriginalValues cannot be used for entities in the Added state." string

Stacktrace:

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

I'm just trying to create a registration page, I don't get it why that I receive this error.

What am I doing wrong here? If this is not enough info that I provided please let me know.

解决方案

Validation may have failed for one of your property values. Might be password for which you have set 'StringLength' to 20 and you are inserting encrypted password. Or might be not passing value for some not null fields.

For debugging and finding actual cause you may use following code block in your 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);
        }
    }
}

这篇关于EntityFramework.dll中出现了“System.Data.Entity.Validation.DbEntityValidationException”类型的第一次机会异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆