c# – 使用Entity Framework进行自引用

c# – 使用Entity Framework进行自引用,第1张

概述代码代表我的问题的小规模: public class Category{ public Guid CategoryID { get; set; } public string Name { get; set; } public Guid? ParentID { get; set; } public bool IsTop { get; set; } publ 代码代表我的问题的小规模:
public class category{    public GuID categoryID { get; set; }    public string name { get; set; }    public GuID? ParentID { get; set; }    public bool Istop { get; set; }    public string Description { get; set; }    public virtual category parentcategory { get; set; }}

当我在Entity Framework中使用此类时,它只生成父类和子类的一个关系.

如何在语义上区分属性,并在sql Server中生成两个不同的关系,用于获取所有子类别(子关系的子项(递归自上而下)),另一个用于获取所有父类别(父项的父项) (递归自下而上))?像这样的东西:

public virtual ICollection<category> childCategorIEs { get; set;} public virtual ICollection<category> parentCategorIEs { get; set;}

我尝试使用模型构建器,但从那里我只能获得一个级别的细节.

解决方法 在我的一个项目中,我遇到了将所有子节点检索到n深度的问题,作为模型中Employee表上的经典主管/员工自引用关系.正如Slauma和Milracle指出的那样,EF不会帮助您在指定父级下检索所有节点到n的深度.但是,我能够在我的存储库中使用 Breadth-first search algorithm来解决这个问题.请注意,我的目标不仅是检索所有子节点,而且要快速执行,因为使用递归liNQ查询需要超过两分钟的时间来进行最高级别的管理.使用此方法,它现在可在不到两秒的时间内执行.
public IEnumerable<string> GetAllSubordinateEmployeeIDsByUserID(string userID){    // RetrIEve only the fIElds that create the self-referencing relationship from all nodes    var employees = (from e in GetAllEmployees()                     select new { e.ID,e.SupervisorID });    // Dictionary with optimal size for searching    Dictionary<string,string> dicEmployees = new Dictionary<string,string>(employees.Count() * 4);    // This queue holds any subordinate employees we find so that we may eventually IDentify their subordinates as well    Queue<string> subordinates = new Queue<string>();    // This List holds the child nodes we're searching for    List<string> subordinateIDs = new List<string>();    // Load the dictionary with all nodes    foreach (var e in employees)    {        dicEmployees.Add(e.ID,e.SupervisorID);    }    // Get the key (employee's ID) for each value (employee's supervisor's ID) that matches the value we passed in    var directReports = (from d in dicEmployees                         where d.Value == userID                         select d.Key);    // Add the child nodes to the queue    foreach (var d in directReports)    {        subordinates.Enqueue(d);    }    // While the queue has a node in it...    while (subordinates.Count > 0)    {        // RetrIEve the children of the next node in the queue        var node = subordinates.Dequeue();        var childNodes = (from e in dicEmployees                          where e.Value == node                          select e.Key);        if (childNodes.Count() != 0)        {            // Add the child nodes to the queue            foreach (var c in childNodes)            {                subordinates.Enqueue(c);            }        }        // Add the node from the queue to the List of child nodes        subordinateIDs.Add(node);    }    return subordinateIDs.AsEnumerable();}

此外,作为脚注,我在这篇Dictionary optimization文章的帮助下,能够提高字典中的查找效率.

总结

以上是内存溢出为你收集整理的c# – 使用Entity Framework进行自引用全部内容,希望文章能够帮你解决c# – 使用Entity Framework进行自引用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1243471.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存