Error Entity Framework | not handled in user code

1

Hello, I need help in the following scenario.

I have a class, where the field is int64 in the backend and in SQL Server the field is bigint.

ObjClasse.VariavelInteira = numInt;
db.SaveChanges();

When saving, the error it presents is:

  

An exception of type   'System.Data.Entity.Validation.DbEntityValidationException' occurred   in EntityFramework.dll but was not handled in user code

How can I identify the error?

    
asked by anonymous 10.01.2017 / 19:41

1 answer

5

This is a generic error saying that some validation failed. To capture the specific error, you can do the following

try
{
    Classe.VariavelInteira = numInt;
    db.SaveChanges();
}
catch (DbEntityValidationException ex)
{    
    var errorMessages = ex.EntityValidationErrors
            .SelectMany(x => x.ValidationErrors)
            .Select(x => x.ErrorMessage);

    var fullErrorMessage = string.Join("; ", errorMessages);         
    var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

    //Use a variável exceptionMessage para ver os erros de validação
}

If after that, you still need help, edit your question with the errors returned.

    
10.01.2017 / 19:51