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()); }



posted @ 2016-10-19 16:12  Lucky-小王子  阅读(374)  评论(0编辑  收藏  举报