如何处理System.Data.Entity.Validation.DbEntityValidationException? [英] How to handle System.Data.Entity.Validation.DbEntityValidationException?

查看:881
本文介绍了如何处理System.Data.Entity.Validation.DbEntityValidationException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序获取以下错误:

My app gets the following error:

类型的例外
  System.Data.Entity.Validation.DbEntityValidationException发生
  在EntityFramework.dll但在用户code没有处理

An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll but was not handled in user code

其他信息:验证失败的一个或多个实体。
  见'EntityValidationErrors'属性的更多细节。

Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

试图注册新用户时,我得到这个错误。错误发生在'db.SaveChanges()

I get this error when trying to register a new user. Error happens on 'db.SaveChanges()'

下面是code:

public ActionResult Registration(x.Models.User user)
        {
            if(ModelState.IsValid)
            {
                using(var db = new xDBEntities1())
                {
                    var crypto = new SimpleCrypto.PBKDF2();
                    var encrpPass = crypto.Compute(user.password);
                    var sysUser = db.users.Create();

                    sysUser.email = user.email;
                    sysUser.username = user.username;
                    sysUser.password = encrpPass;
                    sysUser.premium_credits = 0;
                    sysUser.login_times = 0;
                    sysUser.last_ip = Request.ServerVariables["REMOTE_ADDR"];
                    sysUser.creation_ip = Request.ServerVariables["REMOTE_ADDR"];
                    sysUser.banned = 0;
                    sysUser.creation_date = DateTime.Now;
                    sysUser.creation_time = DateTime.Now.TimeOfDay;

                    db.users.Add(sysUser);
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index", "Home");
        }

编辑:
用户模型类

edit: User model class

public class User
    {
        [Required]
        [StringLength(50)]
        [Display(Name="Username: ")]
        public String username { get; set; }
        [Required]
        [DataType(DataType.Password)]
        [StringLength(50,MinimumLength=6)]
        [Display(Name="Password: ")]
        public string password { get; set; }
        [Required]
        [EmailAddress]
        [StringLength(50)]
        public string email { get; set; }
        public int phonenumber { get; set; }
        public int mobilephonenumber { get; set; }

    }
}

我该如何处理呢?

How can I handle it ?

推荐答案

有某种数据库校验发生$ P $从数据写入到它pventing你的。

There is some sort of database validation happening preventing you from writing the data into it.

该解决方案已在此页面上说:

The solution is already stated on this page:

<一个href=\"http://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert\">Validation失败的一个或多个实体。见'EntityValidationErrors了解详情财产

作为一个额外的音符这是你使用的是.NET MVC你应该使用System.Diagnostics.Debug.WriteLine()而不是Console.Writeline(),当你在调试这将写入调试输出窗口。运行一个MVC项目时,你不能写入控制台。

As an extra note to this as you are using .net mvc you should use System.Diagnostics.Debug.WriteLine() instead of Console.Writeline() and this will write to the debug output window when you are debugging. As you cannot write to the console when running a mvc project.

这篇关于如何处理System.Data.Entity.Validation.DbEntityValidationException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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