views:

45

answers:

1

I'm having some trouble using nHibernate, automapping and a class structure using multiple chains of abstract classes

It's something akin to this

public abstract class AbstractClassA {}

public abstract class AbstractClassB : AbstractClassA {}

public class ClassA : AbstractClassB {}

When I attempt to build these mappings, I receive the following error

"FluentNHibernate.Cfg.FluentConfigurationException was unhandled Message: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

  • Database was not configured through Database method."

However, if I remove the abstract keyword from AbstractClassB, everything works fine. The problem only occurs when I have more than one abstract class in the class hierarchy.

I've manually configured the automapping to include both AbstractClassA and AbstractClassB using the following binding class

public class BindItemBases : IManualBinding
{
    public void Bind(FluentNHibernate.Automapping.AutoPersistenceModel model)
    {
        model.IncludeBase<AbstractClassA>();
        model.IncludeBase<AbstractClassB>();
    }
}

I've had to do a bit of hackery to get around this, but there must be a better way to get this working. Surely nHibernate supports something like this, I just haven't figured out how to configure it right.

Cheers,

James

A: 

Why do you include abstract classes in your AutoMappings, are they presented in the database too? Could you provide the inner exception Fluent throws?

model.IncludeBase<AbstractClassA>();
model.IncludeBase<AbstractClassB>();

With this in place you are trying to map AbstractClassB to the database, which is supposedly not what you want.

HeavyWave
I want to include them because there are a few different classes derived from them. If I remove the abstract, things all work fine, but I'd prefer not to do that. If I don't include the base, then the derived classes will each get a table of their own. I don't want that. If I have ClassA derive directly from AbstractClassA, things work exactly how I want. It's when I add the AbstractClassB in the middle it falls over.
Mr Snuffle
Why don't you just exclude AbstractClassB from mappings, while leaving it in as a base class?
HeavyWave
If I exclude AbstractClassB from the mappings, I get a mapping exception along the lines of 'An association from the table ClassA refers to an unmapped class: AbstractClassB'
Mr Snuffle
I am puzzled. There should not be an association from ClassA to any abstract class from mappings perspective, so check that your AbstractClassB is not getting mapped by fluent.
HeavyWave