views:

590

answers:

4

Is it possible to map entities from multiple assemblies in Fluent NHibernate?

I tried

AutoPersistenceModel
.MapEntitiesFromAssemblyOf<Class1>()
.AddEntityAssembly(assembly)

But it only loads entities from 'assembly' and not from parent assembly of Class1.

EDIT. I figured it out. I had to update Fluent NHibernate to version 1.0, where you can do it like this:

AutoMap
.AssemblyOf<Class1>()
.AddEntityAssembly(typeof(UserEntity).Assembly)
A: 

It seems you may only call AddEntityAssembly once, read here for a discussion.

I would guess it overrides your previous line.

dove
Yep, that's why I'm looking for a way to map 2 assemblies without modifying Fluent.
HeavyWave
A: 

We succesfully map entities from multiple assemblies by using NHibernate.Cfg.Configuration.AddAssembly() multiple times. A code snippet is below. As you can see, we inspect all assemblies in the current domain and any assembly that has our own custom attribute called "HibernatePersistenceAssembly" on it gets added. We created this attribute simply so that this loop would know which assemblies have NHibernate entities in them, but you could use whatever scheme you want to decide which assemblies to add including simply hardwiring them if you wanted to.

In AssemblyInfo.cs for each Assembly that has NHibernate entities in it:

[assembly: HibernatePersistenceAssembly()]

And then in our Hibernate Utilities class:


        public NHibernate.Cfg.Configuration ReloadConfiguration()
        {
            configuration = new NHibernate.Cfg.Configuration();
            configuration.Configure();
            ConfigureConnectionString();
            ConfigureAssemblies();

            return configuration;
        }

        private void ConfigureAssemblies()
        {
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (object attribute in assembly.GetCustomAttributes(true))
                {
                    if (attribute is HibernatePersistenceAssembly)
                        configuration.AddAssembly(assembly);
                }
            }
        }
Clay Fowler
A: 

You could do something similar to what sharp does.

foreach (var assemblyName in mappingAssemblies)
{
    Assembly assembly = Assembly.Load(assemblyName);
    m.FluentMappings.AddFromAssembly(assembly );
}

That works for me at least.

mhenrixon
A: 

I figured it out. I had to update Fluent NHibernate to version 1.0, where you can do it like this:

AutoMap
.AssemblyOf<Class1>()
.AddEntityAssembly(typeof(UserEntity).Assembly)
HeavyWave

related questions