you should probably select the original parent with that id, instead of newing up one.
like Parent x = repository.GetById(44); then, set its x.Child = new List<Child>(); add children/whatever and then save/update parent. i'm not sure if that will help, i'm just speculating of the top of my head.. On Jul 25, 2:33 pm, Andrei Gavrila <[email protected]> wrote: > 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 WCF > service resposible for data access). And everytime I send a parent > object to save, i only have the parent ID and the newly modified > collection. > > Thank you, > Andrei -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
