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

本文关键字:结构 框架 实体 | 更新日期: 2023-09-27 18:37:03

我对实体框架相当陌生,我正在尝试首先使用代码将表转换为树结构,然后使用 OData 发布它。

这是我的数据库表:

-----------------------------------------------------------|身份证 |文本 |HREF |可选 |父标识 ||---------------------------------------------------------|| 0 |商城 |空 |0 |空 || 1 |登录 |#/登录 |1 |0 || 2 |质量 |空 |0 |0 || 3 |任务列表 |#/任务列表 |1 |2 || 4 |结果列表 |#/结果列表 |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 和父 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);

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