try catch 找错误
Posted 代号地狱猫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了try catch 找错误相关的知识,希望对你有一定的参考价值。
出现错误时但又找不到错误在哪里?可以尝试下面的,try catch语句,能清楚的找到error.
try { Repository<AllUsers> superAdmin = new Repository<AllUsers>(context); AllUsers usr = new AllUsers(); usr.Id = "1"; usr.Name = "admin"; usr.Password = "sineva"; usr.IsSuper = true; usr.Description = "系统内置"; usr.IsRCurrent = true; superAdmin.Add(usr); context.SaveChanges(); base.Seed(context); } 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()); }
以上是关于try catch 找错误的主要内容,如果未能解决你的问题,请参考以下文章