然后包含不适用于实体框架LINQ查询 [英] ThenInclude not working for an Entity Framework LINQ query

查看:49
本文介绍了然后包含不适用于实体框架LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据库模型:

I have a database model like this:

public class Customer
{
    public int CustomerId{ get; set; }

    public int OrderId { get; set; }
    public ICollection<Order> Orders { get; set; }
}

public class Order
{
    public int OrderId { get; set; }
    public int Amount { get; set; }

    public int ProductId { get; set; }
    public Product Product { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
}

因此,我们有一个可以下订单的客户.该订单包括产品,其中包括名称.我现在正尝试使用linq语句返回完整的模型,如下所示:

So we have a customer which can do an order. This order includes a product and this includes a name. I'm now trying to return the complete model with a linq statement like the following:

_db.Customer.Include(c => c.Orders).ThenInclude(o => o.Product).SingleOrDefaultAsync();

但是 ThenInclude(o => o.Product)无法正常运行,因为Orders是ICollection.来自德国的问候,在此先感谢您的帮助.

But ThenInclude(o => o.Product) doesn't function because Orders is an ICollection. Greetings from Germany and thanks in advance for any help.

推荐答案

要在EF 6中加载相关实体,应使用 Select 方法,如下所示:

To load related entities in EF 6 you should use Select method as I show below:

_db.Customer.Include(c => c.Orders.Select(o=>o.Product)).SingleOrDefaultAsync();

ThenInclude EF Core的最新版本EF Core中添加了该方法.

ThenInclude method was added in EF Core, which is the last version of EF.

这篇关于然后包含不适用于实体框架LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆