Imagine that I have a Parent/Child relationship managed by NHibernate.
I'm receiving a Parent object from an MVC postback that edits its properties; I want to save just the parent to the database without having to load the children from the database.
At the time of the save, Parent has a Children property that is null (because it hasn't been loaded; there are, however, valid Children in the database for that parent).
When I save a modified Parent (ID=100), NHibernate is issuing a "SET Child.ParentId = NULL WHERE Child.ParentId = 100" statement. I don't want this to happen because there could be valid children. I shouldn't have to load them from the database before the save just to prevent them from being orphaned.
The fluent mappings look like this (true entity names genericized for this post):
public ParentMapping()
{
Table("Parent");
Id(x => x.Id).Column("Id").GeneratedBy.Identity();
Map(x => x.ParentProperty1).Column("ParentProperty1").Not.Nullable();
HasMany(x => x.Children).Cascade.None();
}
public ChildMapping()
{
Table("Children");
Id(x => x.Id).Column("Id").GeneratedBy.Identity();
Map(x => x.ChildProperty1).Column("ChildProperty1").Not.Nullable();
References(x => x.Parent).Column("Parent_Id").Not.Nullable().Fetch.Select();
}
To summarize, I want to save an updated Parent instance that was retrieved from an earlier ISession (and passed to a browser and back through MVC model minding); its Children property is null, but in reality it's got plenty of children in the database. I don't want NHibernate to issue any modifying statements to the Children table at all.
I've experimented with Cascade.None() and LazyLoad() in the hopes that this nudges NHibernate to behave differently, but no luck.
Any insight would be appreciated. Thanks!
Jeff