实体框架自引用表

实体框架自引用表

本文介绍了实体框架自引用表。如何让父母到达root的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自引用表,我需要绑定日期从表到树视图用户控制。我使用BuildThree()方法来获取孩子。我的问题是,如何使用我的View模型获取父母,直到父母使用



我的实体:

  public partial class Category 
{

public Category()
{
this.children = new HashSet< Category>() ;
}

public int CategoryId {get;组; }
public string CategoryName {get;组; }
public Nullable< int> ParentCategoryId {get;组; }

public virtual ICollection< Category>孩子{get;组; }
public virtual Category Parent {get;组; }
}

我的查看模型:

  public class TreeViewModel 
{
public TreeViewModel()
{
this.children = new HashSet< TreeViewModel>();
}

public int id {get;组; }

public string text {get;组; }

public virtual TreeViewModel MyParent {get;组; }

public virtual IEnumerable< TreeViewModel>孩子{get;组; }

}

这是我用来获取孩子的方法

  public IEnumerable< TreeViewModel> BuildThree(IEnumerable< Category>类别,int?parentCategoryId = null)
{
if(categories == null)
return null;
var result = categories.Select(c => new TreeViewModel()
{
id = c.CategoryId,
text = c.CategoryName,

children = BuildThree(c.children,c.CategoryId)
});
返回结果;
}





Database1Entities db = new Database1Entities();
var category = db.Categories.Find(id);

TreeViewModel vm = new TreeViewModel();
vm.id = category.CategoryId;
vm.text = category.CategoryName;

vm.children = BuildThree(category.children);


解决方案

如果您只是寻找一种填充 MyParent propperty,而不必对 BuildThree 方法进行小的更改。

  public IEnumerable< TreeViewModel> BuildThree(IEnumerable< Category>类别,TreeViewModel parentCategory)
{
if(categories == null)
返回null;
var result = new List< TreeViewModel>();
foreach(var category in categories)
{
var treeViewModel = new TreeViewModel();
treeViewModel.Id = category.CategoryId,
treeViewModel.Text = category.CategoryName,
treeViewModel.MyParent = parentCategory;
treeViewModel.Children = BuildThree(category.children,treeViewModel);
result.Add(treeViewModel);
}
返回结果;
}


I have a self referencing table and i need to bind the date from table to tree view user control . I use BuildThree() method to get children. My question is how can i get parent until reach to root parent using my View model

My Entity:

 public partial class Category
        {

            public Category()
            {
                this.children = new HashSet<Category>();
            }

            public int CategoryId { get; set; }
            public string CategoryName { get; set; }
            public Nullable<int> ParentCategoryId { get; set; }

            public virtual ICollection<Category> children { get; set; }
            public virtual Category Parent { get; set; }
        }

my view model:

public class TreeViewModel
{
        public TreeViewModel()
        {
            this.children = new HashSet<TreeViewModel>();
        }

        public int id { get; set; }

        public string text { get; set; }

        public virtual TreeViewModel MyParent { get; set; }

        public virtual IEnumerable<TreeViewModel> children { get; set; }

    }

And this is the method i use to get children

   public IEnumerable<TreeViewModel> BuildThree(IEnumerable<Category> categories, int? parentCategoryId = null)
    {
        if (categories == null)
            return null;
        var result = categories.Select(c => new TreeViewModel()
        {
            id = c.CategoryId,
            text = c.CategoryName,

            children = BuildThree(c.children, c.CategoryId)
        });
        return result;
    }





        Database1Entities db = new Database1Entities();
        var category = db.Categories.Find(id);

        TreeViewModel vm = new TreeViewModel();
        vm.id = category.CategoryId;
        vm.text = category.CategoryName;

        vm.children = BuildThree(category.children);
解决方案

If you just looking for a way to populate MyParent propperty, than have to make small changes of your BuildThree method.

public IEnumerable<TreeViewModel> BuildThree(IEnumerable<Category> categories, TreeViewModel parentCategory)
    {
        if (categories == null)
            return null;
        var result = new List<TreeViewModel>();
        foreach (var category in categories)
        {
           var treeViewModel = new TreeViewModel();
           treeViewModel.Id = category.CategoryId,
           treeViewModel.Text = category.CategoryName,
           treeViewModel.MyParent = parentCategory;
           treeViewModel.Children= BuildThree(category.children,treeViewModel);
           result.Add(treeViewModel);
        }
        return result;
    }

这篇关于实体框架自引用表。如何让父母到达root的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:39