3

I have following code in my asp.net MVC3 application:

string msg = "Beginning report run for: ";
            msg += " Obligor Registry ID: " + obligorID;
            msg += " Requesting Organization Registry ID:" + requestingOrgID;
            msg += " Requesting Agent Registry ID: " + requestingAgentID;

            TransactionLog lg = new TransactionLog();
            lg.TransactionTypeId = 2;
            lg.Message = msg;    


             context.TransactionLogs.Add(lg);
             long referenceNumber = context.SaveChanges();
            return referenceNumber;

and I am getting following error:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. 
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316

3 Answers3

16

While you are in debug mode within the catch {...} block open up the "QuickWatch" window (ctrl+alt+q) and paste in there:

((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors

This will allow you to drill down into the ValidationErrors tree. It's the easiest way I've found to get instant insight into these errors.

GONeale
  • 26,302
  • 21
  • 106
  • 149
2
catch (DbEntityValidationException dbEx)
{
    foreach (var validationErrors in dbEx.EntityValidationErrors)
    {
        foreach (var validationError in validationErrors.ValidationErrors)
        {
            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
        }
    }
}

You need the namespace: System.Data.Entity.Validation

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
1

@GONeale helped me out in this regard. Moreover cause for this exception in my case is that I have certain not null db fields which I didnt included in the partial response update for certain transaction. context.Database.ExecuteSQLCommand would be my suggestion in this case.

zeeshan
  • 39
  • 9