首页 > 代码库 > Entity Framework插入数据报错:Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Entity Framework插入数据报错:Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

stackoverflow的解决方法

        try        {            db.Entity.Add(entity);            db.SaveChanges();        }        catch (DbEntityValidationException dbEx)        {            foreach (var validationErrors in dbEx.EntityValidationErrors)            {                foreach (var validationError in validationErrors.ValidationErrors)                {                    System.Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);                }            }        }

通过调试可以明确的知道哪个字段有问题

如果包含了导航属性,可以查看导航属性的数据,

比如id,那就必须对比下,具体是哪个出了问题

 

Entity Framework插入数据报错:Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.