Monday, April 23, 2012

Fluent NHibernate: Cannot create an instance of AutoMapping X because Type.ContainsGenericParameters is true

This happens probably because you forgot to tell Fluent NHibernate which classes to map explicitly (i.e. its trying to map a class that isn’t one of your entity models).
I usually filter my Fluent NHibernate models overriding the DefaultAutomappingConfiguration:
public class AutoMappingConfig : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        return typeof(EntityBase).IsAssignableFrom(type);
    }
}

And tell Fluent NHibernate about it:
Fluently.Configure()
    .Database(databaseConfig)
    .Mappings(
        m =>
        m.AutoMappings.Add(
            AutoMap.AssemblyOf<User>(new AutoMappingConfig()));