Removing an item from a collection (NHibernate)

I have a parent child relationship between two objects (parent and child).

The display of My Parent is as follows:

<class name="Parent" table="Parents">
    ...
    <bag name="Children" cascade="all">
        <key column="ParentID"></key>
        <one-to-many class="Child"></one-to-many>
    </bag>
</class>

I would like to do the following:

someParent.Children.Remove(someChild);

The Child class has a reference to another Type parent class. Attitude looks like

relation diagram

Note. We apologize for the unrelated url above, I could not miss the asterisk in the url string using Markup

In this regard, when the above code is called, instead of a DELETE query, an UPDATE query is created that removes the ParentID from the Child table (sets it to null).

Is it possible to force NHibernate to completely remove a child entry if it is removed from the Parent.Children collection?

UPDATE

@Spencer Solution

, . - , ( ) , , (CallSessionContext/WebSessionContext) .

@Jamie Solution

, . : entity object

NHibernate update, TypeID ParentID null, . - , , .

@, One-Shot-Delete, . , , .

//Instantiate new collection and add persisted items
List<Child> children = new List<Child>();
children.AddRange(parent.Children);

//Find and remove requested items from new collection
var childrenToRemove = children
    .Where(c => c.Type.TypeID == 1)
    .ToList();

foreach (var c in childrenToRemove) { children.Remove(m); }
parent.Children = null;

//Set persisted collection to new list
parent.Children = Children;

, Jamie . , :

- Inverse = true, Cascade = all

- = , Cascade = all-delete-orphan

, Jamie. , , .

+5
2

, IList<Child>, :

RemoveChild(Child child)
{
    Children.Remove(child);
    child.Parent = null;
    child.Type.RemoveChild(child);
}

Type.RemoveChild , , , RemoveChild.

+1

, , Hibernate , . , - , , , .

. IList ICascadeDeleteChild, , . .

  • , IList IList < > CascadeDeleteList - .
  • .Net- .
  • ICascadeDeleteChild Delete()
  • Delete CascadeDeleteList , . ICascadeDeleteChild, Delete.
  • Child ICascadeDeleteChild.

, , , .

0

All Articles