Entity framework 包括不';不能将LinqKit用作可扩展的

Entity framework 包括不';不能将LinqKit用作可扩展的,entity-framework,ef-core-2.0,linqkit,Entity Framework,Ef Core 2.0,Linqkit,我试图在我的EfCore2.0项目中使用LinqKitAsExpandable,我遇到了这个问题,Includes不起作用 在尝试调试时,我从github下载了LinqKit源代码,并将项目中的Nuget引用替换为项目引用 在调试LinqKit项目时,我注意到对Include的调用没有达到我在ExpandableQueryOfClass.Include上设置的断点 我做了一些进一步的测试,注意到如果我第一次强制转换到ExpandableQueryOfClass(这是我公开的LinqKit中的一个

我试图在我的
EfCore2.0
项目中使用
LinqKit
AsExpandable
,我遇到了这个问题,
Includes
不起作用

在尝试调试时,我从github下载了
LinqKit
源代码,并将项目中的
Nuget
引用替换为项目引用

在调试
LinqKit
项目时,我注意到对
Include
的调用没有达到我在
ExpandableQueryOfClass.Include
上设置的断点

我做了一些进一步的测试,注意到如果我第一次强制转换到
ExpandableQueryOfClass
(这是我公开的
LinqKit
中的一个内部类,因此如果我引用
Nuget
包,我就无法执行强制转换),断点被命中

这是
LinqKit
中的错误还是我做错了什么

这是我的测试代码

using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

using Internal.DAL.Db;
using Internal.Models.Customer;

using LinqKit; // Referencing LinqKit.Microsoft.EntityFrameworkCore
using Xunit;

namespace Internal.EntityFramework.Tests
{
    public class UnitTest1
    {
        private DbContextOptionsBuilder<DataContext> _ctxBuilder =>
            new DbContextOptionsBuilder<DataContext>().UseSqlServer(Connection.String);

        [Fact]
        public async Task SuccessTest()
        {
            using (var ctx = new DataContext(_ctxBuilder.Options))
            {
                var query = 
                    (
                        // this cast is the only difference between the methods
                        (ExpandableQueryOfClass<Order>)
                        ctx.Orders
                        .AsExpandable()
                    )
                    .Include(r => r.Customer)
                    .Take(500);

                var responses = await query.ToListAsync();

                // this succeeds
                Assert.All(responses, r => Assert.NotNull(r.Customer));
            }
        }

        [Fact]
        public async Task FailTest()
        {
            using (var ctx = new DataContext(_ctxBuilder.Options))
            {
                var query = ctx.Orders
                    .AsExpandable()
                    .Include(r => r.Customer)
                    .Take(500);

                var responses = await query.ToListAsync();

                // this fails
                Assert.All(responses, r => Assert.NotNull(r.Customer));
            }
        }
    }
}
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.EntityFrameworkCore;
使用Internal.DAL.Db;
使用内部.Models.Customer;
使用LinqKit;//引用LinqKit.Microsoft.EntityFrameworkCore
使用Xunit;
命名空间Internal.EntityFramework.Tests
{
公共类UnitTest1
{
私有数据库上下文选项生成器\u ctxBuilder=>
新的DbContextOptionsBuilder().UseSqlServer(Connection.String);
[事实]
公共异步任务成功测试()
{
使用(var ctx=newdatacontext(_ctxBuilder.Options))
{
变量查询=
(
//这是两种方法之间唯一的区别
(可扩展查询类)
ctx.订单
.AsExpandable()
)
.包括(r=>r.客户)
.取(500);
var responses=wait query.ToListAsync();
//这成功了
Assert.All(responses,r=>Assert.NotNull(r.Customer));
}
}
[事实]
公共异步任务失败测试()
{
使用(var ctx=newdatacontext(_ctxBuilder.Options))
{
var query=ctx.Orders
.AsExpandable()
.包括(r=>r.客户)
.取(500);
var responses=wait query.ToListAsync();
//这失败了
Assert.All(responses,r=>Assert.NotNull(r.Customer));
}
}
}
}

编辑
2018-05-15
:在LinqKit github存储库中有一个错误。

我不确定这是LinqKit还是EF核心故障(肯定不是您的)

当然,它是由EF核心
Include
/
然后Include
引起的,所有这些都包括对
源的检查。提供者是EntityQueryProvider
,如果
为false,则不执行任何操作

我不确定EF核心设计器的想法是什么-可能是从
EntityQueryProvider
继承的自定义查询提供程序,但同时该类是基础结构的一部分,并标记为不应使用

我也不知道LINQKit计划如何解决这个问题,但正如您所注意到的,当前的实现肯定是坏的/不起作用的。目前,在我看来,它更像一个在制品

目前我看到的唯一解决方法是在includes之后应用
AsExpandable()