简体   繁体   English

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

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

I'm rather new with Entity Framework and I'm trying to convert a table into the tree structure with code first and publish it with OData. 我是Entity Framework的新手,我想先使用代码将表转换为树形结构,然后使用OData发布它。

This is my db table: 这是我的数据库表:

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

What I need is followin JSON: 我需要的是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
            }]
        }]
    }];

For that I have prepared the model: 为此,我准备了模型:

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

and the controller 和控制器

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

This is my GlobalContext class: 这是我的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; }
    }
}

Unfortunately what I get returned back is just the first node, I don't know how to link Id and Parent Id. 不幸的是,我返回的只是第一个节点,我不知道如何链接Id和Parent Id。

Thanks for any help. 谢谢你的帮助。

Add to your object: 添加到您的对象:

public virtual SiteMapConfigurationItem Parent { get; set; }

Then change this: 然后更改此:

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

to this: 对此:

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

So it knows how to traverse up and down the relationship chain. 因此,它知道如何遍历关系链。 Now if you have Lazy Loading enabled on your context you should be able to traverse the self referencing foreign key without any problem. 现在,如果在上下文中启用了“延迟加载”,则应该可以遍历自引用外键,而不会出现任何问题。

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

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