I got this error:
An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
===========================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;
namespace ParkingMvcApp.Models
{
[MetadataType(typeof(ClientMetaData))]
public partial class Client
{
[DataType("Password")]
[System.Web.Mvc.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
public class ClientMetaData
{
[StringLength(10, MinimumLength = 10, ErrorMessage = "Your mobile not corrct")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "You have to enter only numbers 111-111-1111")]
[Required]
[Remote("IsUserNameAvailabe","Account",ErrorMessage ="The mobile already in use")]
public string Mobile { get; set; }
[Required]
[DataType("Password")]
public string Password { get; set; }
[Required]
[DisplayName("First Name")]
public string FirstName { get; set; }
[DisplayName("Created Date")]
public Nullable<System.DateTime> CreatedDate { get; set; }
}
}
=====================================================
[HttpPost]
public ActionResult Register(string Mobile, string Password, string FirstName, int Disability)
{
ParkingContext db = new ParkingContext();
ParkingMvcApp.Models.Client client = new Models.Client();
client.Mobile = Mobile;
client.Role = "C";
client.Password = Password;
client.FirstName = FirstName;
client.CreatedDate = DateTime.Now;
client.TyepOfClientID = Disability;
//To check if the user already regitser
if (db.Clients.Any(m => m.Mobile == Mobile))
{
ModelState.AddModelError("Mobile", "The mobile already in use");
}
if (ModelState.IsValid)
{
db.Clients.Add(client);
db.SaveChanges(); <-----------------------------------Error
return RedirectToAction("Login", "Account");
}
return View(); ;
}
Note: If I remove the compare from the code , the program work as I expected