这篇博文纪录一下:当使用 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 奇怪问题的更多相关文章

  1. EntityFramework 7 Linq Contains In 奇怪问题(已修复)

    问题说明: 博客问题纪录 Use EF7, Linq Contains In is error. EF7 Code Commit adding (client side) support for Co ...

  2. EntityFramework 7 Join Count LongCount 奇怪问题

    先吐槽一下,EF7 目前来说,真对的起现在的版本命名:"EntityFramework": "7.0.0-beta1". 这篇博文纪录一下:当 Linq 查询中 ...

  3. EntityFramework 7 Join Count LongCount 奇怪问题(已修复)

    问题说明: 博客问题纪录 Use EF7, Linq Join Count is error EF7 Code Commit EF7 版本(注意 rc): 旧版本:"EntityFramew ...

  4. EntityFramework 和 linq 判断是否在指定时间段内的方法

    EntityFramework: System.Data.Objects.EntityFunctions.DiffDays(DateTime.Now, inputTime)判断当前时间与指定时间相差多 ...

  5. EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询

    场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...

  6. EntityFramework的linq扩展where

    代码 using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; ...

  7. [EntityFramework]记录Linq中如何比较数据库中Timestamp列的方法(如大于0x00000000000007D1的记录)

    Timestamp对于EF实体的类型是byte[] class Program { static void Main(string[] args) { using (var context = new ...

  8. 爱与恨的抉择:ASP.NET 5+EntityFramework 7

    EF7 的纠缠 ASP.NET 5 的无助 忘不了你的好 一开始列出的这个博文大纲,让我想到了很久之前的一篇博文:恋爱虽易,相处不易:当EntityFramework爱上AutoMapper,只不过这 ...

  9. ASP.NET 5+EntityFramework 7

    爱与恨的抉择:ASP.NET 5+EntityFramework 7   EF7 的纠缠 ASP.NET 5 的无助 忘不了你的好 一开始列出的这个博文大纲,让我想到了很久之前的一篇博文:恋爱虽易,相 ...

随机推荐

  1. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  2. Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Mariadb 10.1.20 + Nginx 1.10.2 + PHP 7.1.0 + Laravel 5.3 )

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...

  3. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  4. VIPM 发布功能总结

    前言 上一篇中,我们分析介绍了LabVIEW自带的安装发布功能,今天总结一下VIPM的发布功能.   VIPM 提到LabVIEW,不能不提VI Package Manager (VIPM)这个工具包 ...

  5. DSY1531*Bank notes

    Description Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我 ...

  6. Centos 编写 service 启动

    Centos 系统服务脚本目录: [html] view plaincopyprint?   /usr/lib/systemd/ 有系统(system)和用户(user)之分, 如需要开机没有登陆情况 ...

  7. apk 反编译

    http://blog.csdn.net/vipzjyno1/article/details/21039349/ [置顶] Android APK反编译就这么简单 详解(附图) 标签: android ...

  8. 在手机网页上模拟 js 控制台

    在手机上模拟 console  做一些简单代码调试 在工作机上编辑好代码用QQ 之类的工具传到 手机上在调试当然你也可以尝试用一只手指写代码的壮举设置 window.console = mobiDeb ...

  9. Angular 1.x 升级到 Angular 2

    原项目用ng1.5写的,现在改成ng2.0了,踩了不少坑,不过都忘记了. 如果你也正好要做这个工作,正好看到这个文章,不妨参考下. AngularJs 1.x -> 2.0 ng-repeat ...

  10. ASP.NET跨平台实践:无需安装Mono的Jexus“独立版”

    在Linux上运行ASP.NET网站或WebApi的传统步骤是,先安装libgdiplus,再安装mono,然后安装Jexus.在这个过程中,虽然安装Jexus是挺简便的一件事,但是安装mono就相对 ...