本文介绍了实体框架4.1检索自引用数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 4.1代码,ASP.NET MVC 3,我正在努力使我的自我引用设置正确。我有一个类别类。它必须自我引用。当表中的ParentCategoryId为空时,类别可以是父类别。如果一个类别有一个值为ParentCategoryId,那么这意味着它属于父类。



我遵循这个 on Code Project。



这是我的类别类:

  public class Category 
{
public int Id {get;组; }
public string Name {get;组; }
public string描述{get;组; }
public string MetaKeywords {get;组; }
public string MetaDescription {get;组; }
public bool IsActive {get;组; }
public virtual Category ParentCategory {get;组; }
public int? ParentCategoryId {get;组;
}

我的上下文类:

  public class PbeContext:DbContext 
{
public DbSet< Category>分类{get;组;

protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
{
dbModelBuilder.Conventions.Remove< PluralizingTableNameConvention>();

dbModelBuilder.Entity< Category>()
.HasOptional(c => c.ParentCategory)
.WithMany()
.HasForeignKey(p => p.ParentCategoryId);
}
}

不知道上述是否正确? >

有人可以帮我取得这个权利吗?我需要的是,当我通过id查询类别时,它必须带回父类(仅在需要时加载)。还必须加载任何子类别(仅在需要时)。我还没有在子类别的类别类别中添加列表。



上面的quesries将如何检索父类和子类的类别?



编辑



这是我如何检索我的类别:

  public Category GetById(int id)
{
return db
.Categories
.Find (ID);
}

由于ParentCategory引用可以为null,我将如何在视图中显示?我有以下内容:

  @ Model.ParentCategory.Name 

..但是如果类别没有与其关联的父类别,它不会给出错误吗?如何在视图中显示?

解决方案

如果您需要访问子类别,可以向您的模型添加集合属性

  public class Category 
{
public int Id {get;组; }
public string Name {get;组; }
public string描述{get;组; }
public string MetaKeywords {get;组; }
public string MetaDescription {get;组; }
public bool IsActive {get;组; }
public virtual Category ParentCategory {get;组; }
public int? ParentCategoryId {get;组; }
public virtual ICollection< Category> ChildCategories {get;组;
}

然后您可以将模型设置为

  dbModelBuilder.Entity< Category>()
.HasOptional(c => c.ParentCategory)
.WithMany(c => ChildCategories)
.HasForeignKey(p => p.ParentCategoryId);

修改



您应该检查 ParentCategory 是否为空。

  @ if(Model.ParentCategory!= null){
< div> @ Model.ParentCategory.Name< / div>
}


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.

I followed this article on Code Project.

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; }
}

My context class:

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?

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.

What would the above quesries look like to retrieve a category with it parent category and child categories?

EDIT

This is how I retrieve my category:

public Category GetById(int id)
{
     return db
          .Categories
          .Find(id);
}

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);

Edit

You should check whether ParentCategory is null or not.

@if(Model.ParentCategory != null){
  <div>@Model.ParentCategory.Name</div>
}

这篇关于实体框架4.1检索自引用数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:42