public ActionResult Create(Employee employee)
 {
     try
     {
         db.Employees.Add(employee);
         // db.Configuration.ValidateOnSaveEnabled = false;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     catch (DbEntityValidationException ex)
     {
         var error = ex.EntityValidationErrors.First().ValidationErrors.First();
         this.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
         ViewBag.DepartmentId = new SelectList(
             db.Departments, "DepartmentId", "Name", employee.DepartmentId);
         return View();
     }
 }
        public ActionResult Edit(Employee employee)
        {
            try
            {
                db.Entry(employee).State = EntityState.Modified;

                // simulate database row being changed by another user
                db.Database.ExecuteSqlCommand(
                    @"UPDATE Employees SET PhoneNumber = 9999, Username = '******' WHERE EmployeeId = @Id",
                    new SqlParameter("Id", 1));

                // property values so we can set breakpoint and see what is going on
                var emp1 = ((Employee)db.Entry(employee).CurrentValues.ToObject());
                var emp2 = ((Employee)db.Entry(employee).OriginalValues.ToObject());
                var emp3 = ((Employee)db.Entry(employee).GetDatabaseValues().ToObject());

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (DbUpdateConcurrencyException ex)
            {
                // note this will not resolve problem here as SQL statement runs every time!
                this.ModelState.AddModelError(string.Empty, "employee data has been changed since you started editing");
                db.Entry(employee).Reload();   // overwrites current values with database values, database wins

                // property values so we can set breakpoint and see what is going on
                var emp1 = ((Employee)db.Entry(employee).CurrentValues.ToObject());
                var emp2 = ((Employee)db.Entry(employee).OriginalValues.ToObject());
                var emp3 = ((Employee)db.Entry(employee).GetDatabaseValues().ToObject());

                ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Name", employee.DepartmentId);
                return View(employee);    // note raw HTML inputs rather than HTML Helpers in view seems to prevent form caching
            }
        }