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

简介: stackoverflow的解决方法 try { db.Entity.Add(entity); db.SaveChanges(); } catch (DbEntityVal...

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,那就必须对比下,具体是哪个出了问题

 

相关文章
|
4月前
Fail to create wsdl definition
Fail to create wsdl definition
38 0
flowable项目报错:java.sql.SQLSyntaxErrorException: Table ‘psr_flowable_test.act_ge_property’ doesn’t exi
flowable项目报错:java.sql.SQLSyntaxErrorException: Table ‘psr_flowable_test.act_ge_property’ doesn’t exi
176 0
使用pageHelper报错 Type definition error: [simple type, classXXXX]
使用pageHelper报错 Type definition error: [simple type, classXXXX]
|
Java 数据格式 XML
JakartaEE Exception: Invalid bound statement (not found): com.mazaiting.blog.dao.UserDao.selectUs...
异常 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.
1216 0
|
Java 数据库连接
【hibernate】错误:org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was altered from xx to xx
所报错误: org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was altered from 40288f8e5ac675fe015ac67725c40001 to ...
1830 0