我试图在我的LinqKit项目中使用AsExpandable,我遇到了一个Includes不能工作的问题。
在尝试调试时,我从github下载了LinqKit源代码,并用项目引用替换了项目中的Nuget引用。
在使用LinqKit项目进行调试时,我注意到我对Include的调用没有触及我在ExpandableQueryOfClass<T>.Include上设置的断点。
我做了一些进一步的测试,注意到如果我第一次转换到ExpandableQueryOfClass (这是我公开的LinqKit中的一个内部类,所以如果我引用Nuget包,就无法进行转换)。
这是LinqKit中的一个bug,还是我做错了什么?
这是我的测试代码。
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存储库上有一个公开发行。
发布于 2018-02-07 12:23:12
我不确定这是LINQKit还是EF核心故障(肯定不是你的)。
当然,这是由EF核心Include / ThenInclude 实现引起的,所有这些都包括对source.Provider is EntityQueryProvider的检查,如果是false,什么也不做。
我不知道EntityQueryProvider的想法是什么--可能是自定义查询提供程序从EntityQueryProvider继承的,但同时类是基础结构的一部分,标记为不应该使用。
我也不知道LINQKit打算如何解决这个问题,但是正如您注意到的,当前的实现肯定是坏的/不起作用的。在我看来,现在它更像是一个WIP。
我现在看到的唯一解决办法是在包含之后应用AsExpandable()。
https://stackoverflow.com/questions/48661790
复制相似问题