Includes 不适用于 LinqKit AsExpandable

jag*_*jag 4 entity-framework linqkit ef-core-2.0

我正在尝试LinqKit AsExpandable在我的EfCore2.0项目中使用,但遇到了Includes不起作用的问题。

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

在调试LinqKit项目时,我注意到我的调用Include没有达到我在 上设置的断点ExpandableQueryOfClass<T>.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));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑2018-05-15:LinqKit github 存储库上有一个未解决的问题

Iva*_*oev 7

我不确定这是 LINQKit 还是 EF Core 的错误(绝对不是你的)。

肯定是由 EF Core Include/ThenInclude 实现引起的,这些实现都包括检查source.Provider is EntityQueryProvider,如果是false.

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

我也不知道 LINQKit 计划如何解决它,但正如您注意到的那样,当前的实现肯定是坏的/不工作的。目前它在我看来更像是一个 WIP。

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