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. python常用第三方库(转载)

    Python标准库与第三方库详解(转载) 转载地址: http://www.codeweblog.com/python%e6%a0%87%e5%87%86%e5%ba%93%e4%b8%8e%e7%a ...

  2. 微信小程序开发之页面跳转并携带参数

    接口: wx.navigateTo({url:......})   保留当前页面,跳转到应用内指定URL页面,导航栏左上角有返回按钮 wx.redirecTo({url:.....})       关 ...

  3. Flutter实战视频-移动电商-54.购物车_商品列表子项布局

    54.购物车_商品列表子项布局 子项做成一个单独的页面 新建cartItem.dart文件 新建cart_page文件夹,在里面新建cart_item.dart页面, 页面名字叫做CartItem 定 ...

  4. FLINK源代码调试方式

    此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 第一种,直接通过IDEA WINDOWS调试 前提是Flink所有依赖已经导入,直接在Test中打断点,然后直 ...

  5. 关于`babel-loader`和`babel-core`版本兼容性问题

    1. 安装babel-loader和babel-core出现问题 1.1 安装babel的转换工具包: npm i babel-core babel-loader babel-plugin-trans ...

  6. css控制页面文字不能被选中user-select:none;

    现象:html中可能有些地方不想让用户复制文字,或是用a标签做了个点击按钮,点快的时候文字会被选中,很丑,这个时候可以使用下面的方案禁止文字选中. 原因:鼠标点快了文字会被选中. 解决方案:不同的浏览 ...

  7. [Xcode 实际操作]九、实用进阶-(20)创建位移关键帧动画:通过添加运动关键点制作位移动画

    目录:[Swift]Xcode实际操作 本文将演示如何通过添加运动关键点的方式,来制作位移动画 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIK ...

  8. 在 UIViewController 中手动增加 TableView 出现 Type 'SomeViewController' does not confirm to protocol 'UITableViewDataSource' 问题的解决办法

    许多时候我们都有在普通的继承自 UIViewController 的控制器中使用 TableView 的需求,这时候就需要当前控制器类继承 UITableViewDelegate 和 UITableV ...

  9. bzoj1139:[POI2009]Wie

    传送门 状压dp,最短路 spfa似乎特别慢 代码: #include<cstdio> #include<iostream> #include<algorithm> ...

  10. fake-useragent

    在编写爬虫进行网页数据的时候,大多数情况下,需要在请求是增加请求头 python下非常好用的伪装请求头的库:fake-useragent,具体使用说明如下: 安装fake-useragent库 pip ...