0

I am trying to update an entity in Entity Framework but I get this error

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Code:

public message getDetailedMessage(int message_id)
{
    message msg = new message();

    if(context.messages.Any(mesg => (mesg.C_From == CurrentUser.Id &&  mesg.id == message_id) || (mesg.Receivers.Any(receiver => receiver.ReceiverId == CurrentUser.Id && receiver.MessageId == message_id))))
    {
        msg = context.messages.Where(mesg => mesg.id == message_id).FirstOrDefault();
        msg.Status = "R";
        context.SaveChanges();
    }
    else
    {
        msg.MDTO.HasError = true;
        msg.MDTO.Error = "You do not have the permission to read this inbox";
    }

    return msg;
}

This is the schema of the table

enter image description here

Could anybody please help me to resolve this error?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mo Haidar
  • 3,748
  • 6
  • 37
  • 76
  • 2
    Check your validation errors... – Janne Matikainen Sep 29 '15 at 08:10
  • did you check the exception message? Check the inner exception. Catch the exception as DbUpdateException and see what you get in the exception. – Ahmed ilyas Sep 29 '15 at 08:11
  • 1
    Inner exception won't help. You can't see EntityValidationErrors in debug mode. You need to catch exception and write a method which will get it for You. Richard's answer will help you: http://stackoverflow.com/questions/10219864/ef-code-first-how-do-i-see-entityvalidationerrors-property-from-the-nuget-pac – MajkeloDev Sep 29 '15 at 08:20
  • Well, for one: your table **doesn't** allow `NULL` for `Title`, `Body`, and `_From` - but you don't seem to be settings those properties on your newly created `msg` object - that would be a place to start! – marc_s Sep 29 '15 at 08:37

0 Answers0