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. 理解复杂的const和typedef和指针的关系

    // container.cpp : 定义控制台应用程序的入口点. //   #include "stdafx.h" #include<iostream> #inclu ...

  2. Eigen中的noalias(): 解决矩阵运算的混淆问题

    作者:@houkai本文为作者原创,转载请注明出处:http://www.cnblogs.com/houkai/p/6349990.html 目录 混淆例子解决混淆问题混淆和component级的操作 ...

  3. TypeScript完全解读(26课时)_4.TypeScript完全解读-接口

    4.TypeScript完全解读-接口 初始化tslint tslint --init:初始化完成后会生成tslint.json的文件 如果我们涉及到一些规则都会在这个rules里面进行配置 安装ts ...

  4. TypeScript完全解读(26课时)_14.ES6和Nodejs中的模块

    创建modules文件夹,我们的文件都写在这里面 modules下面新建index.js文件,在index.ts内引入这个js文件 es6的模块 最主要的两个关键字 import和export imp ...

  5. 15.oauth2 + oidc 实现 server部分

    OAuth主要做授权. OpenIdConnect简历在OAuth2.0基础之上的,相结合 客户端.授权中心.Resource Owner用户本身(资源的拥有者).Resource Server 通过 ...

  6. 电商:html样式集合

    1. <span class="big"  style="text-decoration:line-through;">原价:¥{zlcms:art ...

  7. PHP中的常用正则表达式集锦

    PHP中的常用正则表达式集锦: 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xf ...

  8. 利用StringBuffer来替换内容

    package com.test.java; public class StringBufferTest { public static void main(String[] args) { Stri ...

  9. 1090 Highest Price in Supply Chain (25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  10. python3错误之TypeError: 'dict_items' object is not callable

    这种错误出现在循环结构中套循环结构,而循环时内部循环为字典,外部循环为该字典调用items方法后取到的值,内部循环调用外部循环中遍历的结果: 解决方案: 将外部循环的items()方法调用改为.key ...