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

发布于 2024-11-08 11:24:37 字数 593 浏览 1 评论 0原文

我尝试通过使用多个 .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()

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-11-15 11:24:37

只需指定所有程序集即可。

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

Just specify all of your assemblies.

m.FluentMappings
    .AddFromAssemblyOf(Of AccountMap)()
    .AddFromAssemblyOf(Of SomeOtherMap)();
苯莒 2024-11-15 11:24:37

看起来很多人,包括我自己,都没有找到一个完整的解决方案来添加所有匿名的程序集,并将其放入 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文