FluentNhibernate,添加来自多个程序集的映射

时间:2011-05-19 16:42:16

标签: .net database nhibernate fluent-nhibernate nhibernate-mapping

我尝试通过使用多个.Mappings扩展调用手动添加映射类,但似乎只包括最后一个。那么如何添加几个选定的类映射或多个汇编?

我的流畅配置看起来通常如下:

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

2 个答案:

答案 0 :(得分:7)

只需指定所有装配即可。

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

答案 1 :(得分: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>