Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

来源:互联网 发布:php一句话木马使用 编辑:程序博客网 时间:2024/06/05 23:04
            try            {                // Your code...                // Could also be before try if you know the exception occurs in SaveChanges                context.SaveChanges();            }            //在应用程序中,用下面的版本,调试,查看errMsg            catch (DbEntityValidationException e)            {                string errMsg = string.Empty;                foreach (var eve in e.EntityValidationErrors)                {                    errMsg += string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",                        eve.Entry.Entity.GetType().Name, eve.Entry.State);                    foreach (var ve in eve.ValidationErrors)                    {                        errMsg += string.Format("- Property: \"{0}\", Error: \"{1}\"",                            ve.PropertyName, ve.ErrorMessage);                    }                }                throw;            }            //在控制台,则直接输出            catch (DbEntityValidationException e)            {                foreach (var eve in e.EntityValidationErrors)                {                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",                        eve.Entry.Entity.GetType().Name, eve.Entry.State);                    foreach (var ve in eve.ValidationErrors)                    {                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",                            ve.PropertyName, ve.ErrorMessage);                    }                }                throw;            }