In this project i was trying a Entity Framework Code First Apporach
Table 'Employee' Id int NOT NULL FirstName nvarchar(50) NOT NULL LastName nvarchar(50) NOT NULL MiddleName nvarchar(50) NULL Address nvarchar(300) NULL Salary decimal(16,2) NULL Email nvarchar(50) NULL
It's Perfectly done. Then I Added Entity Framework, Added A EmployeeController
. Two Action Method Index
& Create
with their corresponding Views.
Are were so far perfect.
![enter image description here][1] ![enter image description here][2]
Then I Did the following for Employee Add:
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Employee emp)
{
if (ModelState.IsValid)
{
db.Employees.Add(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(emp);
}
When I Fill the Create
View and Submit It's throwing a DbEntityValidationException
![enter image description here][3]
DbEntityValidationException was unhandled by user code
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.
My Database Table is not updating too, now row created there.
Model definition
public partial class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string Address { get; set; }
public Nullable<decimal> Salary { get; set; }
public string Email { get; set; }
}