• 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:UsersyuezhongxinDesktopEF7srcEF7.TestsEF7_Test.cs:line 83

    相似问题:Calling ICollection<>.Contains() with a sub query doesn't work


    已提交至 EntityFramework 7 issues:Use EF7, Linq Contains In is error.

  • 相关阅读:
    多表关联查询(ORACLE版)
    开发中可能会用到的几个 jQuery 小提示和技巧 (转)
    让jquery easyui datagrid列支持绑定嵌套对象
    java图片处理工具类
    如何为Myeclipse手工添加dtd支持
    二进制与运算
    彻底卸载MYSQL,windows版
    Win7系统安装MySQL5.5.21图解教程
    Rust 资源整理
    【译】我的阅读习惯
  • 原文地址:https://www.cnblogs.com/xishuai/p/ef7-linq-contains-in-error.html
Copyright © 2020-2023  润新知