Removing a tree using NHibernate

I struggle with a little problem and start to conclude that this is simply not possible.

I have a table called Group. As with most of these systems, the group has a collection of ParentGroup and Children. Thus, the group of tables is as follows:

Group
  -ID (PK)
  -Name
  -ParentId (FK)

I made my mappings using FNH AutoMappings, but I had to override the default values ​​for this:

p.References(x => x.Parent)
    .Column("ParentId")
    .Cascade.All();
p.HasMany(x => x.Children)
    .KeyColumn("ParentId")
    .ForeignKeyCascadeOnDelete()
    .Cascade.AllDeleteOrphan()
    .Inverse();

Now the general idea was to remove the node and all its children, which would also be removed by NH. Therefore, deleting a single root node should basically clear the entire table.

At first I tried to use Cascade.AllDeleteOrphan, but this only works to remove items from the Children collection, not to remove the parent.

ForeignKeyCascadeOnDelete, on delete cascade. , MSSql2008 , :

FOREIGN KEY "FKBA21C18E87B9D9F7" "" . . ON UPDATE NO ACTION FOREIGN KEY.

, . , , N + 1. - , , .

+3
1

, .

    public class Node_Map : ClassMap<Node>
    {
        public Node_Map()
        {
            References(x => x.Parent, "IdCmsNodeParent");
            HasMany(x => x.Childs).AsBag()
                                  .Inverse()
                                  .Cascade.Delete()
                                  .KeyColumn("IdNodeParent");
        }
    }

, NHibernate , N + 1. .

Sql 2008, CTE.

Check:

+1

Source: https://habr.com/ru/post/1748650/


All Articles