I’m trying to use the persistence specification testing built into FNH. The CheckList method seems to have been added recently, but the older CheckEnumerable has already been deprecated, so I’m assuming that CheckList is stable.
My test code looks similar to this:
new PersistenceSpecification<Parent>(session)
.CheckProperty(x => x.Foo, 123)
.CheckList(x => x.Children,
new Child[] { new Child { Name = "Bob" } },
(p, c) =>
{
p.Children.Add(c);
c.Parent = p;
})
.VerifyTheMappings();
Note that in the mapping, the Parent owns the relationship (has Cascade.AllDeleteOrphan() and doesn’t have Inverse).
When I run it, I get the dreaded “Cannot insert NULL value into…” SQL Server error message, because NHibernate isn’t setting the ParentId on the Child entity. Same as what happens when you forget to set up the association on both ends.
I checked with a breakpoint and the code inside the lambda isn’t even getting executed, which is obviously why the association isn’t being set properly.
The mappings themselves are totally correct; I can write ordinary code to create and insert an entity just fine. It’s just the CheckList method that I can’t get to work.
What am I doing wrong?
I’m not sure why, but it appears that you do need to use
CheckComponentListinstead ofCheckList. I’m not sure whatCheckListdoes or if it works at all right now, but I looked at the SQL trace andCheckComponentListwas generating the correct statements.CheckComponentListuses the default equality comparer unless one is explicitly specified, which is reference-equality for reference types that do not overrideEquals, so it is critical to either overrideEqualsin the child entity class or use one of theCheckComponentListoverloads that takes anIEqualityComparerargument.