S
S
stcmd042362016-09-17 12:27:41
SQL
stcmd04236, 2016-09-17 12:27:41

How to properly organize and control the hierarchy in the database using the entity framework?

Good day to all! For the first time I work with hierarchy in a DB. In general, it was necessary to organize a group with a hierarchy. And created the following model:

public class MyGroup
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public virtual List<MyGroup> Childrens { get; set; }

        public virtual MyGroup Parent { get; set; }
    }

Everything is correct here (so far I do not see any problems). You can organize a hierarchy. But there will be problems if you add a parent group to the child group. This can be avoided by adding a check to see if this group is a parent for it. But I just can't figure out how to check for those that are at the top of the parent group of the current one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
stcmd04236, 2016-09-17
@stcmd04236

I found a solution, but there is also a minus. Specify nesting level. It seems to solve the problem, but the problem arises if you move a group with nesting of the 3rd level to the first and if it contains several more nested groups.
For example, we have "root group1" (level 0) and contains several groups nested up to level 7. The whole problem starts if we move the group with level 4 to "root group 1". How to deal with child groups of this group. Their nesting level is no longer 5 or 6 or 7 though. What are the ways to solve this problem? It only comes to mind to vyat everyone and change the nesting level to the desired one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question