I'm quite newbie on NHibernate, so sorry if i'm not following right guide for asking.
My question is fairly simple, i've been trying to do something for a long while and now i'm starting to wonder whether is possible or not.
Let's assume we got the usual:
Code:
public class Parent
{
public Parent(){Children = new List<Child>()}
public int ID{get;set;}
public IList<Child> Children{get;set;}
}
and
Code:
public class Child
{
public Child(){}
public int Id{get;set;}
public Parent Parent{get;set;}
}
Let's assume following tables:
Code:
{Parents [int ID]}
{Children [int parentID (FK Parents.ID)] [int ID]}
My question is whether i should take care of having consistency between in memory objects and persisted entities.
Code:
Child childToBeDeleted = ....
Parent parent = ....
parent.Children.Add(childToBeDeleted);
parent.Children.Add(new Child());
parent.Children.Add(new Child());
parent.Children.Add(new Child());
parent.Children.Add(new Child());
//I've checked here parent.Children.Count == 5
session.Save(parent)
//It works until previous line
session.Delete(childToBeDeleted);
//What i would like to have here is (parent.Children.Count == 4) but it's 5 instead
session.Delete(parent)
//previous line is firing an exception
Or i should take care about consistency:
Code:
Child childToBeDeleted = ....
Parent parent = ....
parent.Children.Add(childToBeDeleted);
parent.Children.Add(new Child());
parent.Children.Add(new Child());
parent.Children.Add(new Child());
parent.Children.Add(new Child());
//I've checked here parent.Children.Count == 5
session.Save(parent)
//Here i've made it works
session.Delete(childToBeDeleted);
//////This line is added to keep consistency
parent.Children.Remove(childToBeDeleted);
//////
session.Delete(parent);
I'm really stuck in this point, how shoudl i map this to avoid removing in code (second block). I will invite a beer to anyone who provided me a nice answer