EF Core,DbContext包括相关实体

时间:2019-10-01 18:47:43

标签: dbcontext ef-core-2.2

我有一个数据库实体,其中有几个相关实体。我想通过执行查找或类似操作来查询父实体,但也要包括相关实体。例如,

public class Customer
{
    [Key]
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
    public ICollection<Address> CustomerAddresses { get; }
}

我想查询客户,还包括所有已知地址。通过数据库上下文执行此操作的公认方法是什么?我是否需要先查询Where子句,然后再包含?像这样:

_dbContext.Set()。其中​​(o => o.CustomerId == id).Include(o => o.CustomerAddresses).FirstOrDefault();

0 个答案:

没有答案