views:

418

answers:

1

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?

+4  A: 

I know this has already been sorted out on the mailing list but we might aswell put the answer on stack overflow too.

The PersistenceSpecification class has a CheckList method. Here's an example usage:

_spec.CheckList(x => x.EnumerableOfKittens, kittens, (cat, kitten) => cat.AddKitten(kitten));

This method was only added recently. Get the latest FNH trunk if you can't find it.

Paul Batum
Thanks for the tip!Offtopic: I love SO! If I had asked this elsewhere, I'd probably have gotten at best a RTFM (M from mailing-list :)T've seen other ORM-related projects (ok, just one - SubSonic) talk to their users exclusively on SO, and I think this is a great (albeit unexpected) new usage for SO.
Cristi Diaconescu