实体框架4.1检索自参照数据 [英] Entity Framework 4.1 Retrieving self referencing data
问题描述
我第一次使用实体框架4.1 code和ASP.NET MVC 3,我努力妥善让我自参考设置。我有一个Category类。它必须是自参考到自身。一个类别可以是一个父类别时,在表中的ParentCategoryId为空。如果一个类具有一个值的ParentCategoryId则表示它属于一个父类别
I am using Entity Framework 4.1 code first and ASP.NET MVC 3 and I am struggling to get my self referencing setup properly. I have a Category class. It must be self referencing to itself. A category can be a parent category when the ParentCategoryId in the table is null. If a category has a ParentCategoryId with a value then it means that it belongs to a parent category.
我跟着这个<一个href=\"http://www.$c$cproject.com/Articles/206410/How-to-Configure-a-Self-Referencing-Entity-in-$c$c\">article在code项目。
I followed this article on Code Project.
下面是我的Category类:
Here is my Category class:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
public bool IsActive { get; set; }
public virtual Category ParentCategory { get; set; }
public int? ParentCategoryId { get; set; }
}
我的上下文类:
public class PbeContext : DbContext
{
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
{
dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
dbModelBuilder.Entity<Category>()
.HasOptional(c => c.ParentCategory)
.WithMany()
.HasForeignKey(p => p.ParentCategoryId);
}
}
不知道上面是正确的?
Not sure if the above is correct?
有人可以帮我得到这个权利?我需要的是,当我通过ID查询类别,那么它必须带回父类(仅在需要时加载)。此外,它必须加载所有子类(仅在需要时)。我还没有被添加到列表中的类别类子类别。
Can someone please help me get this right? What I need is that when I query a category by id then it must bring back the parent category (only loaded when needed). Also it must load any child categories (only when needed). I have not yet added a list to the category class for the child categories.
你会上面quesries模样检索类别与它的父类别和子类别?
What would the above quesries look like to retrieve a category with it parent category and child categories?
修改
这是我如何找回我的类别:
This is how I retrieve my category:
public Category GetById(int id)
{
return db
.Categories
.Find(id);
}
由于ParentCategory参考可以为空,我将如何在视图中显示此?我有以下几点:
Because the ParentCategory reference can be null, how would I display this in the view? I have the following:
@Model.ParentCategory.Name
..但不给一个错误,如果该类别有没有与之相关的父类?我怎么会在视图中显示呢?
..but won't it give an error if the category has no parent category associated with it? How would I display it in the view?
推荐答案
如果您需要访问子类可以集合属性添加到模型
If you need to access child categories you can add a collection property to your model
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
public bool IsActive { get; set; }
public virtual Category ParentCategory { get; set; }
public int? ParentCategoryId { get; set; }
public virtual ICollection<Category> ChildCategories{ get; set; }
}
然后你可以设置模型
Then you can setup the model as
dbModelBuilder.Entity<Category>()
.HasOptional(c => c.ParentCategory)
.WithMany(c => ChildCategories)
.HasForeignKey(p => p.ParentCategoryId);
修改
您应该检查 ParentCategory
是否为空或不是。</ P>
You should check whether ParentCategory
is null or not.
@if(Model.ParentCategory != null){
<div>@Model.ParentCategory.Name</div>
}
这篇关于实体框架4.1检索自参照数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!