3

我试图手动添加映射类,通过使用多个.Mappings扩展调用,但它似乎只包括最后一个。那么如何添加几个选定的类映射或多个程序集呢?FluentNhibernate,添加来自多个程序集的映射

我一口流利的配置通常是这样的:

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() 

回答

6

只要指定所有的组件。

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

它看起来像很多人包括我自己在内,没有找到一个完整的解决方案,以添加所有组件在bin文件夹下降,如果他们是匿名的。无论如何,我是这样做的,这不是最优的,但它的解决方案..

了解更多关于NoEntity在这里。

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>