NHibernate Fluent Add external Assembly mappings

。_饼干妹妹 提交于 2019-12-02 10:51:46

AutoMapping

If you want to filter through types, you can use the IAutomappingConfiguration and derive from DefaultAutomappingConfiguration like this:

public class StandardConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        // Entity is the base type of all entities
        return typeof(Entity).IsAssignableFrom(type);
    }
}

You can also use DefaultAutomappingConfiguration if you have no need to filter. But my further example uses the StandardConfiguration.

Change your configuration like this, to populate your types to FluentNHibernate:

SessionFactory = Fluently.Configure(cfg)
    .Mappings(m => MapMyTypes(m))
    .ExposeConfiguration(x => configuration = x)
    .BuildSessionFactory();

And the MapMyTypes method should look like this:

private void MapMyTypes(MappingConfiguration m)
{
    m.AutoMappings.Add(AutoMap.Assemblies(new StandardConfiguration(), 
        Assembly.GetAssembly(typeof(Entity)), 
        Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
    );
}

You can add multiple Assemblies and all get filtered through the StandardConfiguration.

Edit

FluentMappings

It seems that i misread your question. To add mappings you can use a similar method to achieve that but without a IAutomappingConfiguration. Just change the MapMyTypes method to:

private void MapMyTypes(MappingConfiguration m)
{
    m.FluentMappings.AddFromAssembly(Assembly.GetAssembly(typeof(EntityMap)));
}

Combine

You can also combine the FluentMapping and the AutoMapping like this:

private Action<MappingConfiguration> MapMyTypes()
{
    return m =>
    {
        MapFluent(m);
        MapAuto(m);
    };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!