ModelValidationException was unhandled user code

前端 未结 1 1056
野性不改
野性不改 2020-12-10 23:24

Can you help me on the following error, i checked everything no ID mistakes

ModelValidationException was un handled by user code

An exception of type \'Syste

相关标签:
1条回答
  • 2020-12-10 23:51

    Frustratingly .net does not always show you the inner exception. Wrap your code in a try block with this catch

    catch (DbEntityValidationException ex) {
        // Retrieve the error messages as a list of strings.
        var errorMessages = ex.EntityValidationErrors
                .SelectMany(x => x.ValidationErrors)
                .Select(x => x.ErrorMessage);
    
        // Join the list to a single string.
        var fullErrorMessage = string.Join("; ", errorMessages);
    
        // Combine the original exception message with the new one.
        var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
    
        // Throw a new DbEntityValidationException with the improved exception message.
        throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); }
    
    0 讨论(0)
提交回复
热议问题