www.111cn.net 编辑:lanve 来源:转载

今天在处理Entity Framework插入数据库时,报错:

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

一直我一直用Exception ex,这个通用异常处理,一点都差不多哪里出错了。对照实体model和数据库表也都没有一点问题(EF刚开始用,以前都是同事给写好了,我只做前端);

1、解决第一步:

但是,按照他的提示 “See 'EntityValidationErrors' property for more details.” 去 Exception 中查看,却只能看到

并不能看到具体的是那个属性为什么验证不通过,也许不少人都遇到这种情况。

这里给大家介绍一个Exception类,让我们能够轻松的知道具体的哪一个字段出了什么问题。

那就是 System.Data.Entity.Validation.DbEntityValidationException,相信代码都知道怎么写了,最简单的就是

try

{

// 写数据库

}

catch (DbEntityValidationException dbEx)

{

}

在 dbEx 里面中我们就可以看到

这样子我们就能看到 EntityValidationErrors 所有的 ValidationErrors 的详细信息了。

2、去除对插入数据库依赖实体的数据:

我的实体A中有一个   public List<Shop> ShopList { get; set; } 的属性。报错的地方就是说明这个Shop实体的属性都是null。插入的时候shoplist的数据我另作处理,不在这里使用。这样就在service方法中将这个属性设置为null,就不会验证了。

 public tb_WeiXinCard AddCard(tb_WeiXinCard Card)

{

    using (PathAnalysisContext da = new PathAnalysisContext())

    {

string strSql = "";

//if (type == 1)

//{

//    strSql = string.Format("delete from tb_shopcard WHERE AgentId = {0}", id);

//}

StringBuilder sb = new StringBuilder(); 

List<int> listint = new List<int>();

foreach (Shop sp in Card.ShopList)

{

    listint.Add(sp.Id);

}

//Card.ShopList = null;

Card = da.tb_WeiXinCard.Add(Card);

da.SaveChanges();

foreach (int sp in listint)

{ 

    sb.Append(string.Format("insert into    tb_shopcard ([cardid] ,[shopid]) VALUES   ({0} ,{1});", Card.id, sp));

}

strSql = sb.ToString();

da.Database.ExecuteSqlCommand(strSql);

return Card;

    }

}

简单记录一下。。

Entity Framework插入数据报错:Validation failed for one or more entities的更多相关文章

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

    stackoverflow的解决方法 try { db.Entity.Add(entity); db.SaveChanges(); } catch (DbEntityValidationExcepti ...

  2. vs报错找不到错在哪里!Validation failed for one or more entities

    今天在处理Entity Framework修改数据库时,报错: Validation failed for one or more entities. See 'EntityValidationErr ...

  3. EF中,保存实体报错:Validation failed for one or more entities. 如何知道具体错误在哪?

    异常提示:Validation failed for one or more entities. See 'EntityValidationErrors' property for more deta ...

  4. “Validation failed for one or more entities”异常的解决办法

    日志中出现Entity Framework修改数据库时的错误: Validation failed for one or more entities. See 'EntityValidationErr ...

  5. Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法

    Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法 You can extract all the ...

  6. Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法【转载】

    摘自:http://www.cnblogs.com/douqiumiao/default.aspx?opt=msg Validation failed for one or more entities ...

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

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

  8. Entity Framework 插入数据 解决主键非自增问题

    http://blog.csdn.net/educast/article/details/8632806 与Entity Framework相伴的日子痛并快乐着.今天和大家分享一下一个快乐,两个痛苦. ...

  9. mysql插入数据报错1366

    数据表插入中文数据报错 Warning Code : 1366 Incorrect string value: '\xE5\x9C\xA8' for column 'name' at row 1 原因 ...

随机推荐

  1. 【Hadoop】MapReduce笔记(二):MapReduce容错,任务失败处理

    典型问题:Hadoop如何判断一个任务失败?失败了怎么做? 分析:实际情况下,用户代码存在软件错误.进程崩溃.机器故障等都会导致失败.Hadoop判断的失败有不同级别类型,针对不同级别的失败有不同的处 ...

  2. mysql :库操作

    一 系统数据库 information_schema:虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息,列信息, 权限信息, 字符信息等. performance_schema:M ...

  3. POJ 1182 食物链 (破题)

    题意:中文题. 析:对POJ 真是无语,有的题用G++过不了,C++能过,有的题不写EOF(题目明明说就一组的数据的)不过,有的题写EOF也不过, 这个题就是写EOF就过不了... 这个题用的是加权并 ...

  4. 您的Microsof Internet Explorer浏览器包含新版本的内置Adobe Flash Player。

    win8 安装adobe flash player时错误提示: 您的Microsof Internet Explorer浏览器包含新版本的内置Adobe Flash Player. 感谢达人提供了如下 ...

  5. jquery 点击某一行,得到这一行的每个列的数据

    <html><head> <title>test</title> <script src="../Scripts/jquery-1.8. ...

  6. Fitnesse的一个简单实例

    Fixture 代码 package eg; import org.joda.time.DateTime; public class JodaTime { int year; public Strin ...

  7. Flink on Yarn模式启动流程源代码分析

    此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Flink on yarn的启动流程可以参见前面的文章 Flink on Yarn启动流程,下面主要是从源码角 ...

  8. 手游性能优化之深入理解Texture Compression

    http://gad.qq.com/article/detail/7154875 一.引子 手游项目开发日常里,经常有美术同学搞不清Photoshop制图软件与Unity3D游戏引擎之间的图片asse ...

  9. opengl VAO ,VBO

    A Vertex Array Object (VAO) is an object which contains one or more Vertex Buffer Objects and is des ...

  10. springcloud2 (三) 服务治理Eureka及其实现原理

    代码地址:https://gitlab.com/showkawa/architect/tree/master/microservice/eurake 基于springcloud2分析eurake知识点 ...