1.对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性

在EF5.0修改实体的时候,出现“对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性这个错误

修改:

SaveChanges前先关闭验证实体有效性(ValidateOnSaveEnabled)这个开关

 db.Configuration.ValidateOnSaveEnabled = false;
int count = db.SaveChanges();
db.Configuration.ValidateOnSaveEnabled = true;

或者:

/// <summary>
/// 创建 EF上下文 对象,在线程中共享 一个 上下文对象
/// </summary>
/// <returns></returns>
public DbContext GetDbContext()
{
//从当前线程中 获取 EF上下文对象
DbContext dbContext = CallContext.GetData(typeof(DBContextFactory).Name) as DbContext;
if (dbContext == null)
{
dbContext = new IZhanShiEntities();
dbContext.Configuration.ValidateOnSaveEnabled = false;
//将新创建的 ef上下文对象 存入线程
CallContext.SetData(typeof(DBContextFactory).Name, dbContext);
}
return dbContext;
}

2.EF中like怎么查询

See this discussion:

http://stackoverflow.com/questions/1033007/like-operator-in-entity-framework

The relevant part is towards the bottom:

Use SqlFunctions.PatIndex:

http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.patindex.aspx

Like this:

var q = EFContext.Products.Where(x =>
SqlFunctions.PatIndex("%CD%BLUE%", x.ProductName) > 0);

Note: this solution is for SQL-Server only, because it uses non-standard PATINDEX function.

I haven't tried it, but it looks promising.  You would say query = query.Where(w => w.Contacts.Any(c => c.Telephones.Any(p =>   SqlFunctions.PatIndex("%206%555%7777%") > 0  )));


http://blogs.msdn.com/b/eric_erhardt/

地址:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ede2db0-a4f9-4178-84c0-fabf4d3a57fa/how-to-generate-the-sql-like-linq-in-ls-query?forum=lightswitch

EF – 问题集锦的更多相关文章

  1. %E3%80%90%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E3%80%91

    "%3Cdiv%20class%3D%22htmledit_views%22%20id%3D%22content_views%22%3E%0A%20%20%20%20%20%20%20%20 ...

  2. EntityFramework Core查询问题集锦(一)

    前言 和大家脱离了一段时间,有时候总想着时间挤挤总是会有的,但是并非人愿,后面会借助周末的时间来打理博客,如有问题可以在周末私信我或者加我QQ皆可,欢迎和大家一起探讨,本节我们来讨论EF Core中的 ...

  3. 广州.NET微软技术俱乐部 微信群有用信息集锦

    考虑到广州.NET微软技术俱乐部 微信群 十分活跃. 有用信息很有可能被淹没. 所以建立此贴. 首先群的活跃是十分重要的. 所以我是不可能把群搞得像技术论坛和github一样, 因为微信群的定位我在& ...

  4. EntityFramework Core问题处理集锦(一)

    前言 和大家脱离了一段时间,有时候总想着时间挤挤总是会有的,但是并非人愿,后面会借助周末的时间来打理博客,如有问题可以在周末私信我或者加我QQ皆可,欢迎和大家一起探讨,本节我们来讨论EF Core中的 ...

  5. 你必须知道的EF知识和经验

    注意:以下内容如果没有特别申明,默认使用的EF6.0版本,code first模式. 推荐MiniProfiler插件 工欲善其事,必先利其器. 我们使用EF和在很大程度提高了开发速度,不过随之带来的 ...

  6. 看完SQL Server 2014 Q/A答疑集锦:想不升级都难!

    看完SQL Server 2014 Q/A答疑集锦:想不升级都难! 转载自:http://mp.weixin.qq.com/s/5rZCgnMKmJqeC7hbe4CZ_g 本期嘉宾为微软技术中心技术 ...

  7. 采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)

    前言 Entity Framework 延伸系列目录 今天来说说EF与MVC项目的性能检测和监控 首先,先介绍一下今天我们使用的工具吧. MiniProfiler~ 这个东西的介绍如下: MVC Mi ...

  8. 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)

    前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...

  9. Android之常见问题集锦Ⅱ

    Android问题集锦Ⅰ:http://www.cnblogs.com/AndroidJotting/p/4608025.html EditText输入内容改变事件监听 _edit.addTextCh ...

随机推荐

  1. huffman编码压缩算法(转)

    参考:http://blog.csdn.net/sunmenggmail/article/details/7598012 笔试时遇到的一道题.

  2. linux下的shell运算(加、减、乘、除)

    linux下的shell运算(加.减.乘.除) 标签: linuxshell运算加减乘除 2014-03-12 16:25 15127人阅读 评论(0) 收藏 举报 分类: linux(17) ((i ...

  3. mac os x安装php7.0和phalcon3.0

    1,安装xampp(with php7.0) 下载地址: https://www.apachefriends.org/download.html 2,安装phalcon3.0 cd ~/git clo ...

  4. 【GoLang】GoLang 中 make 与 new的区别

    make.new操作 make用于内建类型(map.slice 和channel)的内存分配.new用于各种类型的内存分配. 内建函数new本质上说跟其它语言中的同名函数功能一样:new(T)分配了零 ...

  5. JavaScript——exec和match

    题目17:Read the following javascript code: var someText="web2.0 .net2.0";var pattern=/(\w+)( ...

  6. linux下打包文件或文件夹

    转自:    在linux下如何将文件夹打包                 http://blog.csdn.net/cynhafa/article/details/7303338 linux zi ...

  7. host位置

    windows xp/2003/vista/2008用户HOSTS文件是在“c:\windows\system32\drivers\etc”

  8. Starting MySQL.The server quit without updating PID file (xxxx.pid).[FAILED]

    mysql无法正常启动,查看日志报如下异常 --07T01::.929615Z [ERROR] Fatal error: Please read "Security" sectio ...

  9. libreoffice安装

    centos7下libreoffice的安装 #下载安装包wget http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/5.1.1/rpm/x86_64 ...

  10. Delphi中Format与FormatDateTime函数详解

    copy:http://hi.baidu.com/yunfanleo/blog/item/0c51d9cdbc0531550eb34558.html Format是一个很常用,却又似乎很烦的方法,本人 ...