实体框架 - 从表更改为树结构

时间:2015-01-09 09:05:35

标签: c# asp.net entity-framework odata

我对Entity Framework很新,我试图先将代码转换为树形结构,然后用OData将其发布。

这是我的数据库表:

-----------------------------------------------------------
| id | text        | href         | selectable | parentId |
|---------------------------------------------------------|
|  0 | MES         | NULL         | 0          | NULL     |
|  1 | Login       | #/login      | 1          | 0        |
|  2 | Quality     | NULL         | 0          | 0        |
|  3 | Task List   | #/taskList   | 1          | 2        |
|  4 | Result List | #/resultList | 1          | 2        |
-----------------------------------------------------------

我需要的是跟随JSON:

var tree = [
    {
        text: "MES",
        selectable: false,
        nodes: [
        {
            text: "Login",
            href: "#/login",
            selectable: true
        },
        {
            text: "Quality",
            selectable: false,
            nodes: [
            {
                text: "Task List",
                href: "#/taskList",
                selectable: true
            }, {
                text: "Result List",
                href: "#/resultList",
                selectable: true
            }]
        }]
    }];

为此,我准备了模型:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace AtslMde5Service.Models
{
    [Table("SiteMap")]
    public class SiteMapConfigurationItem
    {
        [Key]
        [Column("id")]
        public int Id { get; set; }
        [Column("text")]
        public string Text { get; set; }
        [Column("href")]
        public string Href { get; set; }
        [Column("selectable")]
        public bool Selectable { get; set; }
        [Column("parentId")]
        public int? ParentId { get; set; }

        public virtual ICollection<SiteMapConfigurationItem> ChildNodes { get; set; }
    }
}

和控制器

using AtslMde5Service.Models;
using System.Linq;
using System.Web.Http;
using System.Web.Http.OData;

namespace AtslMde5Service.Controllers.OData
{
    public class SiteMapConfigurationItemsController : ODataController
    {
        private GlobalContext db = new GlobalContext();

        [EnableQuery]
        public SingleResult<SiteMapConfigurationItem> GetSiteMapConfigurationItems()
        {
            return SingleResult.Create(db.SiteMapConfigurationItems.Where(siteMapConfigurationItem => siteMapConfigurationItem.ParentId == null));
        }
    }
}

这是我的GlobalContext类:

using AtslMde5Service.Models.ATSL_MDE;
using System.Data.Entity;

namespace AtslMde5Service.Models
{
    public class GlobalContext : DbContext
    {
        public GlobalContext()
            : base("name=GlobalContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<SiteMapConfigurationItem>().HasMany(k => k.ChildNodes);

            modelBuilder.Entity<SiteMapConfigurationItem>().Map(m =>
            {
                m.MapInheritedProperties();
            });
        }

        public DbSet<SiteMapConfigurationItem> SiteMapConfigurationItems { get; set; }
    }
}

不幸的是,我返回的只是第一个节点,我不知道如何链接Id和Parent Id。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

添加到您的对象:

public virtual SiteMapConfigurationItem Parent { get; set; }

然后改变这个:

modelBuilder.Entity<SiteMapConfigurationItem>().HasMany(k => k.ChildNodes);

到此:

modelBuilder.Entity<SiteMapConfigurationItem>().HasMany(k => k.ChildNodes).WithOptional(k => k.Parent).HasForeignKey(k => k.ParentId);

因此它知道如何遍历关系链。现在,如果您在上下文中启用了延迟加载,则应该能够毫无问题地遍历自引用外键。