I can't figure out how (or if it's at all possible) to use the Fluent NHibernate PersistenceSpecification<T>.CheckList(...)
method to check an inverse one-to-many mapping.
Here's what I'm trying to do:
Using fluent nhibernate on a vanilla blog/post example, I have defined a one-to-many blog<->posts mapping. One restriction I want is that a post must ALWAYS point to the blog it corresponds to, right from the moment it is added to the database. (basically, the BlogID column in the Posts table is NOT NULL)
For this reason, I defined the mapping like this:
public class BlogMap: ClassMap<Blog>
{
public BlogMap(){
[...]
HasMany(x => x.Posts)
.Inverse()
.Cascade.All();
}
The Blog class has a AddPosts(IList<Post> posts)
method which takes care of both sides of the mapping, so when the Post instances are INSERTed in the database, their BlogID column is already populated with the corresponding Blog ID.
So far, so good.
The problem is, when using
new PersistenceSpecification<Blog>(GetSession(), eq)
.CheckList(c => c.Posts,
new [] {new Post{Name = "Post 1"}, new Post{Name = "Post 2"}})
.VerifyTheMappings();
in a mapping unit-test, I get this exception:
"System.ApplicationException: Actual count does not equal expected count"
...which is to be expected, since the BlogID value of the posts is not set anywhere.
I'm wondering if I can somehow get access to the Blog
instance created behind the scenes by the PersistenceSpecification
, so I can manually call the AddBlogs(...) method before doing the checks.
If I remove the .Inverse()
from the mapping (and also remove the database NOT NULL constraint for the BlogID column), the test passes.
Any ideas?