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

 

相关文章
|
XML 数据格式
SAP Fiori Elements - object detail batch roundtrip triggered by binding property in embedded xml vie
SAP Fiori Elements - object detail batch roundtrip triggered by binding property in embedded xml vie
107 0
SAP Fiori Elements - object detail batch roundtrip triggered by binding property in embedded xml vie
How to update BOL entity property value via ABAP code
How to update BOL entity property value via ABAP code
112 0
How to update BOL entity property value via ABAP code
|
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.
1209 0
|
Java 数据库连接
hibernate提示Unknown entity: :xxx
错误提示: org.hibernate.MappingException: Unknown entity: org.dao.po.Role at org.hibernate.internal.SessionFactoryImpl.
2208 0