locked
problem Validation failed for one or more entities RRS feed

  • Question

  • User-1440430215 posted

    hey everyone i have this problem show when i try to register a new user

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

     here's my modle

    [Table("UserTb")]
        public class UserTb
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public Guid Id{ get; set; }
    
            [Required]
            [RegularExpression(@"^[^\\/:*|,;\.\)\(]+$", ErrorMessage = "The characters ':', '.' ';', '*', '/' and '\' are not allowed")]
            [Remote("IsUserExists", "Accounts", ErrorMessage = "User Name already in use")]
            public string Name { get; set; }
    
            [Required]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string PassUser { get; set; }
    
            [NotMapped]
            [DataType(DataType.Password)]
            [Display(Name = "Confirm password")]
            [System.ComponentModel.DataAnnotations.Compare("PassUser", ErrorMessage = "The password and confirmation password do not match.")]
            public string ConfirmPassword { get; set; }
    
            [Required]
            [Remote("IsEmailExists", "Accounts", ErrorMessage = "Email already in use")]
            [EmailAddress(ErrorMessage = "Invalid Email Address")]
            public string Email { get; set; }
            [Required]
            public string Mobile { get; set; }
    
            public string PassUserSalt { get; set; }
    
    
        }

    and this my code

    PropertieDBContext db = new PropertieDBContext();
            public JsonResult IsUserExists(string Name )
            {
                //check if any of the UserName matches the UserName specified in the Parameter using the ANY extension method.  
                return Json(!db.UserTb.Any(x => x.Name == Name), JsonRequestBehavior.AllowGet);
            }
    
            public JsonResult Checkcurrentpassword(string currentpassword)
            {
                
                return Json(!db.UserTb.Any(x => x.PassUser != currentpassword), JsonRequestBehavior.AllowGet);
            }
    
            [AllowAnonymous]
            public JsonResult IsEmailExists(string Email)
            {
                //check if any of the UserName matches the UserName specified in the Parameter using the ANY extension method.  
                return Json(!db.UserTb.Any(x => x.Email == Email), JsonRequestBehavior.AllowGet);
            }
    
    
    
            // GET: Accounts
            public ActionResult Index()
            {
                return View();
            }
    
            [AllowAnonymous]
            [HttpGet]
            public ActionResult Register()
            {
    
                return View();
            }
    
            [AllowAnonymous]
            [HttpPost]
            public ActionResult Register(UserTb U)
            {
                var crypto = new SimpleCrypto.PBKDF2();
                var encpass = crypto.Compute(U.PassUser);
                U.PassUser = encpass;
                U.PassUserSalt = crypto.Salt;
                db.UserTb.Add(U);
                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }

    i even try to do this

    [AllowAnonymous]
            [HttpPost]
            public ActionResult Register(UserTb U)
            {
                try
                {
                    var crypto = new SimpleCrypto.PBKDF2();
                    var encpass = crypto.Compute(U.PassUser);
                    U.PassUser = encpass;
                    U.PassUserSalt = crypto.Salt;
                    db.UserTb.Add(U);
                    db.SaveChanges();
                    return RedirectToAction("Index", "Home");
                }
                catch
                {
                    return RedirectToAction("Index", "Home");
                }
            }

    but no registration happens 

    Monday, January 2, 2017 4:24 PM

Answers

  • User1771544211 posted

    Hi dmw,

    dmw2200

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

    This exception indicates that something isn’t valid and if you want to find out what that is then you should attach a debugger and inspect the exception.

    The following link shows how to trace the exact error of this exception, please take it as reference.

    http://www.dotnetpiper.com/2014/08/validation-failed-for-one-or-more.html

    And you can validate the User Model before your save it into database by using the following code.

    [AllowAnonymous]
    [HttpPost]
    public ActionResult Register(UserTb U)
    {
        try
        {
            //validate the model
            if (ModelState.IsValid)
            {
                var crypto = new SimpleCrypto.PBKDF2();
                var encpass = crypto.Compute(U.PassUser);
                U.PassUser = encpass;
                U.PassUserSalt = crypto.Salt;
                db.UserTb.Add(U);
                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View(U);
            }
        catch
        {
            return RedirectToAction("Index", "Home");
        }
    }

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 3, 2017 2:16 AM

All replies

  • User1771544211 posted

    Hi dmw,

    dmw2200

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

    This exception indicates that something isn’t valid and if you want to find out what that is then you should attach a debugger and inspect the exception.

    The following link shows how to trace the exact error of this exception, please take it as reference.

    http://www.dotnetpiper.com/2014/08/validation-failed-for-one-or-more.html

    And you can validate the User Model before your save it into database by using the following code.

    [AllowAnonymous]
    [HttpPost]
    public ActionResult Register(UserTb U)
    {
        try
        {
            //validate the model
            if (ModelState.IsValid)
            {
                var crypto = new SimpleCrypto.PBKDF2();
                var encpass = crypto.Compute(U.PassUser);
                U.PassUser = encpass;
                U.PassUserSalt = crypto.Salt;
                db.UserTb.Add(U);
                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View(U);
            }
        catch
        {
            return RedirectToAction("Index", "Home");
        }
    }

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 3, 2017 2:16 AM
  • User-1440430215 posted

    thanks that was so usefully for detected error of this Validation

    Tuesday, January 3, 2017 3:43 AM