4

我正在尝试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));
            }
        }
    }
}

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

4

1 回答 1

7

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

可以肯定的是,它是由 EF Core Include/ ThenInclude implementations引起的,它们都包括检查,source.Provider is EntityQueryProvider如果是false.

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

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

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

于 2018-02-07T12:23:12.120 回答