I (my classes) have a parent child releationship and I want to the child only to know the id of the parent.
That way if i only need the details of one child not the complete parent with the rest of the object graph will be loaded.
Because I have foreign key constraints active in the db I get violations when I save. To solve I need this two step approach:
class Child
{
public virtual Guid IdParent { get; set; }
}
class Parent
{
void AddChild(Child c)
{
c.IdParent = this.Id;
this.childs.Add(c);
}
}
var parent = new Parent()
session.Save(parent) // now the parent has an id
var child = new Child();
parent.Add(child); // child now knows the parent id
session.Save(child);
What I really want is
var parent = new Parent();
var child = new Child()
parent.AddChild(child);
session.Save(parent);
Is there a way to store the parent Id at the child after nhibernate knows the id? I also want to have foreign key checking active in the db.
Mapping of parent:
this.HasMany<Child>(x => x.childs)
.Not.LazyLoad()
.AsBag()
.Inverse()
.Cascade.AllDeleteOrphan()
.KeyColumn("id_parent");