bretddog
bretddog

Reputation: 5519

FluentNhibernate, add mappings from multiple assemblies

I've tried to add mapping classes manually, by using multiple .Mappings extension calls, but it seems only to include the last one. So how do I add several selected class maps, or multiple assemblies?

My fluent configuration looks typically like this:

 Return Fluently.Configure() _
                .Database(SQLiteConfiguration.Standard.ConnectionString(connectionString) _
                .Cache(Function(c) c.UseQueryCache())) _
            .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of AccountMap)() _
                .Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never())) _
            .ExposeConfiguration(Function(c) InlineAssignHelper(cfg, c)) _
            .BuildSessionFactory()

Upvotes: 4

Views: 4436

Answers (2)

Bootproject
Bootproject

Reputation: 94

It looks like a lot of people including myself, doesn't find a complete solution to add all assemblies dropped in bin folder if they are anonymous. Anyhow, i did it like this, it's not optimal but its a solution..

Read more about NoEntity here.

    private static Conf CreateConfig()
    {
        return Fluently.Configure()
            .Database(DatabaseConfig)
            .Mappings(AddAssemblies)                
            .ExposeConfiguration(ValidateSchema)
            .ExposeConfiguration(BuildSchema)
            .BuildConfiguration();
    }

    private static void AddAssemblies(MappingConfiguration fmc)
    {
         (from a in AppDomain.CurrentDomain.GetAssemblies()
                select a
                    into assemblies
                    select assemblies)
                    .ToList()
                    .ForEach(a => 
                     {
                        //Maybe you need to inly include your NameSpace here.
                        //if(a.FullName.StartsWith("MyAssembly.Name")){
                        fmc.AutoMappings.Add(AutoMap.Assembly(a)
                            .OverrideAll(p => 
                            {
                                p.SkipProperty(typeof(NoEntity));
                            })
                            .Where(IsEntity));
                     }
          );
    }

    private static bool IsEntity(Type t)
    {
        return typeof(IEntity).IsAssignableFrom(t);
    }

    //Map IEntity
    public class User : IEntity{}
    public class UserMap : Entity<User>{}
    //UserMap inherits ClassMap<T>

Upvotes: 2

Vadim
Vadim

Reputation: 17955

Just specify all of your assemblies.

m.FluentMappings
    .AddFromAssemblyOf(Of AccountMap)()
    .AddFromAssemblyOf(Of SomeOtherMap)();

Upvotes: 8

Related Questions