[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "StaffID,StartDate,EndDate,LeaveType,NoOfDays,AppStatus,HiddenID,LeaveReason,UpdateDate,UpdatedBy")] CurrentApplication currentApplication, Staff staff)
{
if (ModelState.IsValid)
{
db.CurrentApplications.Add(currentApplication);
currentApplication.UpdateDate = System.DateTime.Now;
currentApplication.AppStatus = "PENDING";
currentApplication.UpdatedBy = User.Identity.Name;
var model = new LeaveIndexData();
var userEmail = User.Identity.Name;
model.Staffs = db.Staffs.Single(i => i.Email == userEmail);
var userID = model.Staffs.StaffID;
currentApplication.StaffID = userID;
//decimal period = (currentApplication.StartDate.Date - currentApplication.EndDate.Date).TotalDays;
//currentApplication.NoOfDays = period;
TimeSpan tSpan = (currentApplication.EndDate.Value).Subtract(currentApplication.StartDate.Value);
currentApplication.NoOfDays = tSpan.Days + 1;
db.Staffs.Add(staff);
staff.BalanceLeave = staff.BalanceLeave - currentApplication.NoOfDays;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(currentApplication);
}
My Create above is giving me the error Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
It was fine and working perfectly until I added these two lines:
db.Staffs.Add(staff);
staff.BalanceLeave = staff.BalanceLeave - currentApplication.NoOfDays;
I thought it would be fine to do it this way since I added in the db.Staffs the same way as I did for CurrentApplications but it unexpectedly gave me the error. I'm not sure how to resolve it either.
My Staff model looks like this:
public partial class Staff
{
public int StaffID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Nullable<decimal> AllocatedLeave { get; set; }
public Nullable<decimal> BalanceLeave { get; set; }
}
I know there are similar questions but those are about how to find the error. I know what the error is now but I'm unable to figure out why I'm having it because it seems correct to me. Please help! Thank you :)
EntityValidationErrors
for better details on what is happening? This will be much better (than guessing) to see what is going wrong. – adricadar((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors
. Hereex
is exception object. – Rahul Nikateinner exception
in quick watch. – Rahul Nikate