asp.net MVC 3 - 你如何加载一个实体的儿童和盛大的孩子吗? [英] asp.net mvc 3 - How do you load children AND grand-children of an entity?
问题描述
我想知道如何加载实体的多个水平;更具体如何加载一个单一的实体的后代多级。下面是我创建了一个例子。假设一个Web界面上,创建一个新的产品后,我想,以显示其类别类型。因此,我们有某一类的产品,而这个类别都有一个类别与类型的名称:
I would like to know how to load multiple level of entities; more specifically how to load multiple level of descendants of a single entity. Here is an example I created. Suppose that on a web interface, after creating a new product, I would like to display its category type. So we have a product of a certain category, and this category has a category type with a name:
[HttpPost]
public ActionResult DoSomething(Product product)
{
if (ModelState.IsValid)
{
productRepository.InsertOrUpdate(product);
productRepository.Save();
...
context.Entry(product).Reference(p => p.Category).Load();
string someString1 = product.Category.SomeProperty;
...
string someString2 = product.Category.CategoryType.Name;
...
有人告诉我如何在另一篇文章中(见someString1在上面的例子中)在保存对象加载后的孩子,但我无法弄清楚如何加载盛大儿童(someString2)。
Someone showed me how to load children after saving an object on another post (see someString1 in the example above), but I cannot figure out how to load grand-children (someString2).
有<一个href=\"http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx\"相对=nofollow>在网络上的例子,说明如何加载实体的集合,所有相关的对象,而是要说明如何加载单个实体的所有后代的省略。
There are examples on the net showing how to load a collection of entities and all related objects, but the omit to show how to load all the descendants of a single entity.
我使用ASP.Net MVC 3 EF 4.1。而这一切是相当新的给我,所以请多多包涵。
I am using ASP.Net MVC 3 with EF 4.1. And all this is quite new to me, so please bear with me.
推荐答案
您可以得到它像这样
context.Entry(product).Reference(p => p.Category).Load();
string someString1 = product.Category.SomeProperty;
...
context.Entry(product.Category).Reference(c => c.CategoryType).Load();
string someString2 = product.Category.CategoryType.Name;
这篇关于asp.net MVC 3 - 你如何加载一个实体的儿童和盛大的孩子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!