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

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

问题描述

我的应用程序会收到以下错误:

My app gets the following error:


类型为
'System.Data.Entity.Validation.DbEntityValidationException的异常'在EntityFramework.dll中发生
,但未在用户代码中处理

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()'

这是代码:

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

    }
}

我该如何处理?

推荐答案

有一些数据库验证发生,阻止您将数据写入其中。

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

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

The solution is already stated on this page:

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

作为使用.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天全站免登陆