EntityFramework 7 Linq Contains In 奇怪问题
这篇博文纪录一下:当使用 EF7,Linq 实现类似 where filename in('','','') SQL 代码,使用 Contains 出现报错问题。
project.json 配置文件(EF7 最新版本):
{
"version": "1.0.0-*",
"dependencies": {
"EntityFramework": "7.0.0-beta2-11758",
"EntityFramework.SqlServer": "7.0.0-beta2-11758",
"EntityFramework.Commands": "7.0.0-beta2-11758"
},
"frameworks": {
"aspnet50": {
"dependencies": {
}
},
"aspnetcore50": {
"dependencies": {
"System.Runtime": "4.0.20-beta-22231"
}
}
}
}
BloggingContext 配置代码:
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata;
using System.Collections.Generic;
namespace EF7
{
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<BlogCate> BlogCates { get; set; }
protected override void OnConfiguring(DbContextOptions builder)
{
builder.UseSqlServer(@"Server=.;Database=Blogging;Trusted_Connection=True;");
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Blog>()
.Key(b => b.BlogId);
builder.Entity<BlogCate>()
.Key(b => b.CateId);
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int BlogCateId { get; set; }
}
public class BlogCate
{
public int CateId { get; set; }
public string CateName { get; set; }
}
}
测试代码:
[Fact]
public void TestWithContains()
{
using (var context = new BloggingContext())
{
var values = new[] { "a", "b", "c" };
var query = from b in context.Blogs
where values.Contains(b.Url)
select b;
var result = query.ToList();
}
}
EF6 测试结果:

EF6 生成 SQL 代码:
SELECT
[Extent1].[BlogId] AS [BlogId],
[Extent1].[BlogCateId] AS [BlogCateId],
[Extent1].[Url] AS [Url]
FROM [dbo].[Blog] AS [Extent1]
WHERE ([Extent1].[Url] IN (N'a', N'b', N'c')) AND ([Extent1].[Url] IS NOT NULL)
EF7 测试结果:

详细异常信息:
Remotion.Linq.Clauses.ResultOperators.ContainsResultOperator
at Microsoft.Data.Entity.Query.ResultOperatorHandler.HandleResultOperator(EntityQueryModelVisitor entityQueryModelVisitor, ResultOperatorBase resultOperator, QueryModel queryModel)
at Microsoft.Data.Entity.Relational.Query.RelationalResultOperatorHandler.HandlerContext.get_EvalOnClient()
at Microsoft.Data.Entity.Relational.Query.RelationalResultOperatorHandler.HandleResultOperator(EntityQueryModelVisitor entityQueryModelVisitor, ResultOperatorBase resultOperator, QueryModel queryModel)
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, Int32 index)
at Remotion.Linq.Clauses.ResultOperatorBase.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
at Remotion.Linq.QueryModelVisitorBase.VisitResultOperators(ObservableCollection1 resultOperators, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.Data.Entity.Query.ExpressionTreeVisitors.DefaultQueryExpressionTreeVisitor.VisitSubQueryExpression(SubQueryExpression subQueryExpression)
at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource)
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
at Microsoft.Data.Entity.Relational.Query.RelationalQueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
at Remotion.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection1 bodyClauses, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult] (QueryModel queryModel)
at Microsoft.Data.Entity.Relational.RelationalDataStore.Query[TResult] (QueryModel queryModel)
at Microsoft.Data.Entity.Query.EntityQueryExecutor.ExecuteCollection[T](QueryModel queryModel)
at Remotion.Linq.Clauses.StreamedData.StreamedSequenceInfo.ExecuteCollectionQueryModel[T] (QueryModel queryModel, IQueryExecutor executor)
at Remotion.Linq.Clauses.StreamedData.StreamedSequenceInfo.ExecuteQueryModel(QueryModel queryModel, IQueryExecutor executor)
at Remotion.Linq.QueryModel.Execute(IQueryExecutor executor)
at Remotion.Linq.QueryProviderBase.Execute(Expression expression)
at Remotion.Linq.QueryProviderBase.System.Linq.IQueryProvider.Execute[TResult] (Expression expression)
at Remotion.Linq.QueryableBase1.GetEnumerator()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource] (IEnumerable1 source)
at EF7.Tests.EF7_Test.TestWithContains() in C:\Users\yuezhongxin\Desktop\EF7\src\EF7.Tests\EF7_Test.cs:line 83
相似问题:Calling ICollection<>.Contains() with a sub query doesn't work
已提交至 EntityFramework 7 issues:Use EF7, Linq Contains In is error.
EntityFramework 7 Linq Contains In 奇怪问题的更多相关文章
- EntityFramework 7 Linq Contains In 奇怪问题(已修复)
问题说明: 博客问题纪录 Use EF7, Linq Contains In is error. EF7 Code Commit adding (client side) support for Co ...
- EntityFramework 7 Join Count LongCount 奇怪问题
先吐槽一下,EF7 目前来说,真对的起现在的版本命名:"EntityFramework": "7.0.0-beta1". 这篇博文纪录一下:当 Linq 查询中 ...
- EntityFramework 7 Join Count LongCount 奇怪问题(已修复)
问题说明: 博客问题纪录 Use EF7, Linq Join Count is error EF7 Code Commit EF7 版本(注意 rc): 旧版本:"EntityFramew ...
- EntityFramework 和 linq 判断是否在指定时间段内的方法
EntityFramework: System.Data.Objects.EntityFunctions.DiffDays(DateTime.Now, inputTime)判断当前时间与指定时间相差多 ...
- EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询
场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...
- EntityFramework的linq扩展where
代码 using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; ...
- [EntityFramework]记录Linq中如何比较数据库中Timestamp列的方法(如大于0x00000000000007D1的记录)
Timestamp对于EF实体的类型是byte[] class Program { static void Main(string[] args) { using (var context = new ...
- 爱与恨的抉择:ASP.NET 5+EntityFramework 7
EF7 的纠缠 ASP.NET 5 的无助 忘不了你的好 一开始列出的这个博文大纲,让我想到了很久之前的一篇博文:恋爱虽易,相处不易:当EntityFramework爱上AutoMapper,只不过这 ...
- ASP.NET 5+EntityFramework 7
爱与恨的抉择:ASP.NET 5+EntityFramework 7 EF7 的纠缠 ASP.NET 5 的无助 忘不了你的好 一开始列出的这个博文大纲,让我想到了很久之前的一篇博文:恋爱虽易,相 ...
随机推荐
- MySQL表名和数据库关键字相同解决办法
今天改他们的代码的时候,遇到了MySQL表名和数据库关键字的问题. 由于表名是关键字,导致增删改查都报错. Hibernate: select leave0_.id as id22_, leave0_ ...
- chm文件索引丢失和不能搜索
regsvr32 hhctrl.ocx regsvr32 itss.dll regsvr32 itircl.dll
- 汉字正则表达式[\u4E00-\u9FFF]原因
转载易天:正则表达式的汉字匹配 这里是几个主要非英文语系字符范围 2E80-33FFh:中日韩符号区.收容康熙字典部首.中日韩辅助部首.注音符号.日本假名.韩文音符,中日韩的符号.标点.带圈或带括符文 ...
- asp.net 验证码(一)Session
1.模板页 //创建网页模板 输入验证码文本框 并且将文本框中的内容发送的后端验证中去 <p>请输入验证码:<input type="text" name=&qu ...
- php-(/usr/local/php)安装编译选项
./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --enable-fpm \ ...
- Swift库运行崩溃
报错如下: 解决方法: 退出 Xcode 找到 DerivedData 文件夹 删除 (路径: ~/Library/Developer/Xcode/DerivedData) 删除 com.apple. ...
- linux时间同步ntp服务的安装与配置
1.首先安装NTP [root@localhost /]# yum install ntp -y 2.修改NTP配置文件,添加NTP服务器的网络位置 /etc/ntp.conf # For mo ...
- "SQL Server does not handle comparison of NText, Text, Xml, or Image data types."
"SQL Server does not handle comparison of NText, Text, Xml, or Image data types." sql2000 ...
- 《Linux内核设计与实现》读书笔记 第三章 进程管理
第三章进程管理 进程是Unix操作系统抽象概念中最基本的一种.我们拥有操作系统就是为了运行用户程序,因此,进程管理就是所有操作系统的心脏所在. 3.1进程 概念: 进程:处于执行期的程序.但不仅局限于 ...
- ABP理论学习之OData集成(新增)
返回总目录 本篇目录 介绍 安装 创建控制器 例子 样例项目 介绍 OData在其官网的定义是: 允许以一种 简单且标准的方式创建和使用可查询的.可互操作的RESTful APIs. 在ABP中也可以 ...