包含不适用于LinqKit AsExpandable

时间:2018-02-07 10:43:30

标签: entity-framework ef-core-2.0 linqkit

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

在尝试调试时,我从github下载了LinqKit源代码,并用项目引用替换了项目中的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));
            }
        }
    }
}

编辑2018-05-15:LinqKit github存储库中有open issue

1 个答案:

答案 0 :(得分:2)

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

确定它是由EF Core Include / ThenInclude implementations引起的,所有这些都包含对source.Provider is EntityQueryProvider的检查,并且如果&#39}则不执行任何操作; s false

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

我也不知道LINQKit是如何计划解决它的,但是你注意到当前的实现肯定会被破坏/无法正常工作。目前它看起来更像是WIP。

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