Search code examples
c#.netnhibernatefluent-nhibernatenhibernate-mapping

Configure FluentNHibernate, FluentMappings.AddFromAssembly; meaning


The line

    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Product>()

What does it do? Will it look for any class in the assembly of the Product class that derives from the ClassMap ? Or what is the logic behind? Can I just put any random class of that assembly here and expect it to find all mapping classes in this assembly?

    private static ISessionFactory CreateSessionFactory()
    { 
          return Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
            .ConnectionString(Properties.Settings.Default.FnhDbString)
            .Cache(c => c
                .UseQueryCache()).ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Product>()
            .Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never()))
            .BuildSessionFactory();
    }

Solution

  • I always thought that when you use AddFromAssemblyOf, fluent will try to map EVERY class in the assembly.

    Therefore you just need to add a class (any one) from an assembly that contains your ClassMap.

    Additional from the fluent wiki

    ..it then adds any fluent mappings from the assembly that contains YourEntity