繁体   English   中英

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

[英]Entity Framework - change from table to tree structure

我是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        |
-----------------------------------------------------------

我需要的是Followin 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。

谢谢你的帮助。

添加到您的对象:

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

因此,它知道如何遍历关系链。 现在,如果在上下文中启用了“延迟加载”,则应该可以遍历自引用外键,而不会出现任何问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM