EF提交插入数据catch捕获具体异常方法

try { db.SaveChanges(); }
catch (DbEntityValidationException ex)
{
StringBuilder errors = new StringBuilder();
IEnumerable<DbEntityValidationResult> validationResult = ex.EntityValidationErrors;
foreach (DbEntityValidationResult result in validationResult)
{
ICollection<DbValidationError> validationError = result.ValidationErrors;
foreach (DbValidationError err in validationError)
{
errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n");
}
}
Console.WriteLine(errors.ToString());
}

posted @ 2018-03-27 15:09  举个栗子oO  阅读(920)  评论(0编辑  收藏  举报