Search code examples
nhibernatefluent-nhibernateautomapping

Fluent nHibernate Auto Mapping - Issue with AutoMapping Override


I've just tried to get a project up and running with Fluent Automapping (I'm familiar with Fluent but used to write each of the maps)

I have an object ScriptType which has a ParseRules property

public class ScriptType : EntityBase
{
    public virtual string Name { get; set; }
    public virtual IList<ParseRule> ParseRules { get; set; }
}

This is being Auto Mapped as HasMany and I wanted References.

I therefore added an AutoMapping override to another assembly ...

public class ScriptTypeOverride : IAutoMappingOverride<ScriptType>
{
    public void Override(AutoMapping<ScriptType> mapping)
    {
        mapping.References(x => x.ParseRules);
    }
}

And altered my configuration as so ...

return configuration
            .Mappings(m => m.AutoMappings
                               .Add(AutoMap.AssemblyOf<DatabaseInfo>()
                                        .IgnoreBase<EntityBase>()
                                        .Conventions.AddFromAssemblyOf<KeyConvention>()
                                        .UseOverridesFromAssemblyOf<ScriptTypeOverride>()));

But I get this .... :(

An association from the table ScriptType refers to an unmapped class: System.Collections.Generic.IList`1[[GIT.ScriptWizard.Entities.ParseRule ...

Can anyone help please?


Solution

  • References is for creating many-to-one relationships between two entities, and is applied on the "many side." You're referencing a single other entity, so you use the References method. HasMany is the "other side" of the References relationship, and gets applied on the "one side."

    From Fluent's website.

    How should your relation work? It looks like a classic one ScriptType-to-many ParseRules, so this should be HasMany on ScriptType's side, as Fluent does.

    Maybe, if you want to have bidirectional relationship here, where ParseRule's side is the "owning" side of the relation, you should use Inverse() in ScriptType.ParseRules mapping override.