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>