ModelValidationException was unhandled user code

守給你的承諾、 提交于 2019-11-28 11:49:42

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); }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!