Hello,
I have the following Parent-Child relationship model:
public class Parent { public virtual int? ID { get; set; } public virtual string Name { get; set; }
public virtual IList<Child> Child { get; set; } }
public class Child { public virtual int? ID { get; set; } public virtual string Name { get; set; } }
<class name="Parent" table="Parent"> <id name="ID" column="ID" type="int" > <generator class="sequence"> <param name="sequence">SQ_PARENT</param> </generator> </id> <property column="PNAME" name="Name" /> <bag name="Child" cascade="all-delete-orphan"> <key column="PARENTID"/> <one-to-many class="Child, NHibernateOneToMany"/> </bag> </class>
<class name="Child" table="Child"> <id name="ID" column="ID" type="int" > <generator class="sequence"> <param name="sequence">SQ_CHILD</param> </generator> </id> <property column="CNAME" name="Name" /> </class>
And 2 test methods.
[TestMethod()] public void Save_Parent() { IParentRepository repository = new ParentRepository(); Parent dbProduct = new Parent() { Name = "P5", }; dbProduct.Child = new List<Child>(); dbProduct.Child.Add(new Child() { Name = "A_C_1" }); dbProduct.Child.Add(new Child() { Name = "A_C_2" });
repository.Add(dbProduct); }
[TestMethod()] public void Save_Parent_2() { Parent x = new Parent() { ID = 44, Name = "P5" }; x.Child = new List<Child>(); x.Child.Add(new Child() { Name = "A_C_NEW" }); IParentRepository repository = new ParentRepository(); repository.Add(x); }
In the second test, I assigned my parent the ID that resulted on the first test.
The db outcome is: * one parent in db * one linked child with the name A_C_NEW * 2 unlinked childs with names: A_C_1, A_C_2 (the PARENT_ID column is null)
I would like to know if there is any way of deleting the unlinked childs.
The logic above models the persistence logic of my app. I have a parent-child tree in the database, that is serialized by a WCF Service and sent to a WPF client that edits the parent-child tree (removes childs, adds childs) and afterwards the client serializes the tree and sends it back to the WCF that should save it to the database.
Thank you, Andrei
|