I have this one-to-many association I created zilions of times with 'old' nhibernate or fluent. But I cann't make it work with mapping-by-code
These are the classes
public class Parent
{
public virtual IList<Child> Children { get; set; }
}
public class Child
{
public virtual Parent Parent { get; set; }
}
Nothing odd
and these are mappings classes
For Parent:
Bag(x => x.Parent, m => m.Key(k => k.Column("Parent_id")));
Child:
ManyToOne(x => x.Children, map => { map.Column("Parent_id"); map.Cascade(Cascade.All); });
If I do the following
var parent = new Parent();
parent.Children.Add(new Child());
session.SaveOrUpdate(parent);
I got correct INSERT for parent, but it does an UPDATE for any child added
UPDATE TableChildren
......
WHERE Id = 0 <-????
What's am I missing? I'm banging my head!!
I see two issues. The mapping seems to be inverted (Bag should go for Children, ManyToOne for a Parent). The essential setting here is also the inverse="true".
As in detail documented here:
The Children should be mapped like this:
Bag(x => x.Children, m =>
m.Inverse(true);
m.Cascade(Cascade.All);
m.Key(k => k.Column("Parent_id")));
And the Parent like this
ManyToOne(x => x.Parent, map =>
{
map.Column("Parent_id");
});
The inverse="true" is a way how to instruct NHibernate, that each child can manage itself. So, once the child is added into the Children collection, we also have to set its Parent! NHibernate will then INSERT the child with the correct reference in one step.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With