EntityFramework 7 Linq Contains In 奇怪问题

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 测试结果:

EntityFramework 7 Linq Contains In 奇怪问题

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 测试结果:

EntityFramework 7 Linq Contains In 奇怪问题

详细异常信息:

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.ExecuteCollectionT
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



本文转自田园里的蟋蟀博客园博客,原文链接:http://www.cnblogs.com/xishuai/p/ef7-linq-contains-in-error.html,如需转载请自行联系原作者

上一篇:HTML样式编辑


下一篇:SpringBoot整合Swagger2