2
    [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 :)

Community
  • 1
  • 1
  • Can you see the `EntityValidationErrors` for better details on what is happening? This will be much better (than guessing) to see what is going wrong. – adricadar Dec 15 '15 at 08:24
  • Check the error in Quick Watch with this `((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors`. Here `ex` is exception object. – Rahul Nikate Dec 15 '15 at 08:36
  • @RahulNikate I got the error end of expression expected on the two problematic lines I have mentioned above ._. – Cucumber Ninja Dec 15 '15 at 09:35
  • @CucumberNinja I don't see detail error in your question. You need to check `inner exception` in quick watch. – Rahul Nikate Dec 15 '15 at 09:52

2 Answers2

0

I think this will help you what exactly is wrong with your validation, and you can also output some human readable message to the user.

 try
    {
      Db.SaveChanges();
    }
    catch(DbEntityValidationException ex){
      foreach(var property in ex.EntityValidationErrors)
      {

        string entityName=eve.Entry.Entity.GetType().Name;

        foreach(var error in property.ValidationErrors)
        {
          Debug.WriteLine("Table name: "+entityName+"  "+ error.PropertyName+" : "+error.ErrorMessage);
        }
    }
}
Ivelin Ivanov
  • 148
  • 3
  • 14
0

To view EntityValidationErrors add the following watch expression to your wacth window:

((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors
Mohammad Zare
  • 1,489
  • 7
  • 25
  • 45