0

I've inherited a large set of NHibernate mappings that live in an existing, functional application. I've branched this application to develop some new features, and while I do so I'm also extending the testing infrastructure to allow for a more TDD-like approach. But now I've hit a wall in one of my integration tests...

I have a class with test data, which I insert prior to the integration test. In the method that inserts these, I get the following exception:

NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of Domain.Entities.Project ---> System.InvalidCastException: Unable to cast object of type 'System.Object' to type 'Domain.Entities.ProjectModules'.

and I can't figure out why. I have two Project instances that I try to persist in the database on setup, both defined like this:

new Project("2023", "projeName", "projaddr")
{
    PrincipalOwner = UserOne, // UserOne and Office are other properties
    Office = Office,
    // I've tried just not instantiating this too - gave the same exception
    ProjectModules = new ProjectModules 
    {
        HasModuleOne = false,
        HasModuleTwo = false
    });
});

The (relevant part of the) Fluent NHibernate mapping looks like this:

Component(m => m.ProjectModules, c =>
{
    c.LazyLoad();
    c.Map(x => x.HasModuleOne)
        .Column("ModuleOne").Not.Nullable().Default("0");
    c.Map(x => x.HasModuleTwo)
        .Column("ModuleTwo").Not.Nullable().Default("0");
});
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • Just curious but is this a new mapping you added or was this a previous mapping that is not working under a new version of Fluent NH? Or was this old code that just never worked at all? – Cole W Jul 06 '11 at 13:53
  • @Cole: This mapping is old, and I haven't changed either that or the version of FNH. I don't think there's ever been working integration tests like the one I'm trying to build, and I've created new test data (using the old classes). But these mappings are used, with the same version of FNH, in a production environment without the users complaining :P – Tomas Aschan Jul 06 '11 at 14:03
  • Nice! With that being said users are creating new Projects in production then right? If so is there a difference in how they are creating the objects? Unless they aren't creating these objects in production I wouldn't see any reason why your integration test would be different. If it is different maybe spotting those differences will help lead you to the answer. – Cole W Jul 06 '11 at 14:19
  • @Cole: Hm... turns out the save code for projects in the application doesn't use NHibernate after all - the projects are inserted using a stored procedure with some other legacy DAL that we're trying to migrate away from... – Tomas Aschan Jul 06 '11 at 15:41

1 Answers1

1

I've solved this - for some reason, NHibernate didn't like when the component mapping was specified inline in the mapping for Projects, but if I moved the mapping to a separate class ComponentMap<T> it worked. So I changed the problematic lines to

Component(p => p.ProjectModules);

and added the following class to my mappings assembly:

public class ProjectModulesMap : ComponentMap<ProjectModules>
{
    LazyLoad.Always();
    Map(pm => pm.ModuleOne);
    Map(pm => pm.ModuleTwo);
}

Then everything worked as I would have expected it to from the start.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402