简体   繁体   English

我如何获得一个实体及其所有相关实体

[英]How do I get an entity and all of its related entities

I am using EF6 and I am trying to get a list of entities. 我正在使用EF6,并且正在尝试获取实体列表。 For each entity in the list i want it to fill in the child entities in the one call. 对于列表中的每个实体,我希望它在一次调用中填写子实体。

For instance: 例如:

Order.Id
Order.ColletionOfItems

Item.Id
Item.OrderId
Item.ProductName
Item.CollectionOfOptions

Option.Name
Option.Value

using(var db = DbContext)
{   //I want to fill in everything during this call as I am using all of it in the
    //The calling function.
    OrderList = db.Orders.Select().include //This is where I am stuck
    return OrderList;
}

I want the returned collection to have all the orders, all the items associated to the individual order and all the options associated to an individual item. 我希望返回的集合具有所有订单,与单个订单关联的所有项目以及与单个项目关联的所有选项。

How do I build my linq statement to do this? 我该如何构建linq语句来做到这一点? Is there a better way then the .Include("MyMajicString") ? 有没有比.Include("MyMajicString")更好的方法? What should I actual search for because my searches have led to very few acceptable responses? 我的搜索导致几乎没有可接受的答复,我应该实际搜索什么?

You need to use the Include extension method: 您需要使用Include扩展方法:

using(var db = new YourContext())
{   
    var OrderList = db.Orders.Include(o=>o.ColletionOfItems.Select(i=>i.CollectionOfOptions));
    return OrderList;
}

You can also use the DbQuery.Include method which receive as parameter the path of the nav. 您还可以使用DbQuery.Include方法,该方法接收导航的路径作为参数。 properties you want to load as an string : 要作为string加载的属性:

using(var db = new YourContext())
{   
    var OrderList = db.Orders.Include("ColletionOfItems.CollectionOfOptions");
    return OrderList;
}

But is better use the first one because is strongly typed. 但是最好使用第一个,因为它是强类型的。 With this second method you can make a mistake typing the name of one of the properties or you could change the name of the nav. 使用第二种方法,您可能会在键入属性之一的名称时出错,或者可以更改导航的名称。 properties in your model in the future and forget rename the property in the path. 将来在模型中添加属性,而忘记在路径中重命名该属性。

Another thing, be careful loading large amounts of data. 另一件事,请小心加载大量数据。 With that query, you are going to load all orders and the related properties, so, try to filter first (calling the Where method after the Include call) before load the info you need to your application. 使用该查询,您将加载所有订单和相关属性,因此,在将所需信息加载到应用程序之前,请尝试先进行过滤(在Include调用之后调用Where方法)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除与特定实体相关的所有实体? - How do I delete all the entities related to a specific entity? 如何获得在linq中嵌套到实体的相关实体? 调查问题,子问题和选项 - How do I get related entity nested in linq to entities? Survey questions, subquestions, and options LINQ to Entities-将所有相关实体字段转换为字符串 - LINQ to Entities - get all related entity field into a string 如何在 Entity Framework LINQ To Entities 中进行联合? - How can I do a Union all in Entity Framework LINQ To Entities? 如何使用实体框架从数据库视图加载相关实体? - How do I load related entities from a database view using entity framework? 如何使用 Entity Framework 和 .NET 5.0 构建查询以在相关表中查找不匹配的实体? - How do I contruct a query to find unmatched entities in related table with Entity Framework and .NET 5.0? 如何从映射到实体的存储过程导航相关实体 - How do I navigate related entities from a stored procedure that is mapped to an entity 使用实体框架代码优先方法,如何创建一个具有相关实体列表作为子属性的实体? - Using Entity Framework Code First approach, how do I create an entity that has a list of related entities as a child property? 使用实体框架返回所有相关实体 - Return all related entities with Entity Framework 实体框架5-获取不相关/可导航的实体 - Entity Framework 5 - Get non related / navigable entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM