使用具有自己的外部/导航键的Model类 [英] Working with a Model class that has a foreign/navigation key to itself
问题描述
public class Category
{
public int CategoryID {get;组; }
public string CategoryName {get;组; }
public int? ParentCategoryID {get;组; }
public string CategoryDesc {get;组; }
[ForeignKey(ParentCategoryID)]
public virtual Category ParentCategory {get;组; }
public virtual ICollection< Product>产品{get;组; }
}
问题:我无法理解为我如何与这个课程一起工作。在使用和传递以下代码到视图
var cat = dbStore.Categories.Include(ParentCategory)。ToList() code>。
我收到此错误:对象引用未设置为对象的实例。这是因为根类别具有null ParentCategoryID。请告诉我如何使用这个代码或任何可以帮助我理解在这种情况下工作的资源。只要任何类型的代码将有助于使用上述模型,如显示列表或菜单或任何东西,只是任何东西。
通常你做的是从顶级类别到底级类别。为了做到这一点,你需要在你的课上定义 SubCategories
集合
public class Category
{
public int CategoryID {get;组; }
public string CategoryName {get;组; }
public int? ParentCategoryID {get;组; }
public string CategoryDesc {get;组; }
[ForeignKey(ParentCategoryID)]
public virtual Category ParentCategory {get;组; }
[InverseProperty(ParentCategory)]
public virtual ICollection< Category>子类{get;组; }
public virtual ICollection< Product>产品{get;组;
}
然后,您检索顶级类别
var topCategories = dbStore.Categories
.Where(category => category.ParentCategoryID == null)
.Include(category => ; category.SubCategories).ToList();
之后,您可以遍历hierachey
foreach(top top类别中的var topCategory)
{
//使用顶级类别
foreach(topCategory.SubCategories中的var子类别)
{
}
}
I am trying to develop a catalog project in ASP.NET MVC 3 and using EF Code first with an existing Database. There is a Categories table in my database that points to itself. For that, I have written the following model class. --"Correct me if the model is wrong"--
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public int? ParentCategoryID { get; set; }
public string CategoryDesc { get; set; }
[ForeignKey("ParentCategoryID")]
public virtual Category ParentCategory { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Question : I am unable to understand as to how can i work with this class. While using and passing the following code to the view
var cat = dbStore.Categories.Include("ParentCategory").ToList()
.
I got this error : Object reference not set to an instance of an object. This is happening because the root category has null ParentCategoryID. Please tell me how will you work with this code or any resource that can help me understand working in such scenarios. Just any sort of code will be helpful that uses the above the model, like displaying a list or a menu or anything, just anything.
Usually what you do is travel from top level categories to bottom level categories. Inorder to do that first you need to define SubCategories
collection in your class
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public int? ParentCategoryID { get; set; }
public string CategoryDesc { get; set; }
[ForeignKey("ParentCategoryID")]
public virtual Category ParentCategory { get; set; }
[InverseProperty("ParentCategory")]
public virtual ICollection<Category> SubCategories{ get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Then you retrieve the top level categories
var topCategories = dbStore.Categories
.Where(category => category.ParentCategoryID == null)
.Include(category => category.SubCategories).ToList();
After that you can traverse the hierachey
foreach(var topCategory in topCategories)
{
//use top category
foreach(var subCategory in topCategory.SubCategories)
{
}
}
这篇关于使用具有自己的外部/导航键的Model类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!