Mar*_*the 5 c# linq entity-framework-core
我有一个这样的数据库模型:
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; }
}
Run Code Online (Sandbox Code Playgroud)
因此,我们有一个可以下订单的客户。该订单包括产品,其中包括名称。我现在正尝试使用linq语句返回完整的模型,如下所示:
_db.Customer.Include(c => c.Orders).ThenInclude(o => o.Product).SingleOrDefaultAsync();
Run Code Online (Sandbox Code Playgroud)
但是ThenInclude(o => o.Product)不起作用,因为Orders是ICollection。来自德国的问候,在此先感谢您的帮助。
要在 EF 6 中加载相关实体,您应该使用Select如下所示的方法:
_db.Customer.Include(c => c.Orders.Select(o=>o.Product)).SingleOrDefaultAsync();
Run Code Online (Sandbox Code Playgroud)
ThenInclude 方法是在 EF Core 中添加的,这是 EF 的最后一个版本。
| 归档时间: |
|
| 查看次数: |
5765 次 |
| 最近记录: |