首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含不适用于LinqKit AsExpandable

包含不适用于LinqKit AsExpandable
EN

Stack Overflow用户
提问于 2018-02-07 10:43:29
回答 1查看 1.6K关注 0票数 4

我试图在我的LinqKit项目中使用AsExpandable,我遇到了一个Includes不能工作的问题。

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

在使用LinqKit项目进行调试时,我注意到我对Include的调用没有触及我在ExpandableQueryOfClass<T>.Include上设置的断点。

我做了一些进一步的测试,注意到如果我第一次转换到ExpandableQueryOfClass (这是我公开的LinqKit中的一个内部类,所以如果我引用Nuget包,就无法进行转换)。

这是LinqKit中的一个bug,还是我做错了什么?

这是我的测试代码。

代码语言:javascript
复制
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存储库上有一个公开发行

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-07 12:23:12

我不确定这是LINQKit还是EF核心故障(肯定不是你的)。

当然,这是由EF核心Include / ThenInclude 实现引起的,所有这些都包括对source.Provider is EntityQueryProvider的检查,如果是false,什么也不做。

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

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

我现在看到的唯一解决办法是在包含之后应用AsExpandable()

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48661790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档