Fluent Nhibernate-多个独立程序集中的类映射
考虑以下Fluent配置Fluent Nhibernate-多个独立程序集中的类映射,nhibernate,fluent-nhibernate,mapping,Nhibernate,Fluent Nhibernate,Mapping,考虑以下Fluent配置 FluentConfiguration config = Fluently.Configure(); config.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008.ConnectionString(ConfigurationManager.ConnectionStrings[dbKey].ConnectionString)); // MemberMap is i
FluentConfiguration config = Fluently.Configure();
config.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008.ConnectionString(ConfigurationManager.ConnectionStrings[dbKey].ConnectionString));
// MemberMap is in the same assembly as this class, contains
// maps for member and role entities as part of a default
// membership service provider
MappingAssemblies.Add(typeof(MemberMap).Assembly);
foreach (Assembly mappingAssembly in MappingAssemblies)
{
// For each assembly that has been added to MappingAssemblies
// we add to the current mapping configuration. This allows
// me to drop this helper into any solution and it provide
// standardized membership abilities AS WELL AS manage
// the solutions unique ClassMaps
config.Mappings(m =>
m.FluentMappings.AddFromAssembly(mappingAssembly)
);
}
if (exportSchema)
{
config.ExposeConfiguration(cfg =>
{
new SchemaExport(cfg)
.Create(true, true);
}
);
}
_sessionFactory = config.BuildSessionFactory();
该逻辑保存在静态类中,我在应用程序启动时从Global.asax中调用该类。启动配置将类似于
Database.MappingAssemblies.Add(typeof(PageContentMap).Assembly);
// This is the method detailed above
Database.FluentConfigureSessionFactory("MySolutionsDb", true);
因此,我的想法是将我的成员和角色实体对象打包到与数据库助手对象相同的程序集中,这样我想要创建的任何解决方案都可以立即获得我的标准化成员资格能力,并且能够简单地创建自己的解决方案特定的类映射,并将它们添加到配置对象中
问题是,熟悉的呼唤
config.Mappings(m =>
m.FluentMappings.AddFromAssembly(mappingAssembly)
);
似乎只能处理单个组件。添加到列表中的内容无关紧要,只会映射最后添加的程序集。作为上述方法的一种替代方法,我尝试保留对
MappingConfiguration
(这是config.Mappings(m=>)
中的“m”代表的内容)的引用,但这也不起作用。很明显,这样调用m.FluentMappings.AddFromAssembly
或任何FluentMappings.Add
方法都会覆盖以前存在的内容,但肯定有办法做到这一点吗?这似乎不是一个很“奇怪”的要求。老问题,但我在看了这个之后设法解决了它,所以我将尝试回答它。我就是这样做的(ForEach()是NHibernate.Linq的扩展):
我也必须为自动映射的东西做这个,那里的语法有点不同。我有一个接口,用于标记我要自动映射的所有类:
config.Mappings(m =>
m.AutoMappings.Add(AutoMap.Assemblies(MappingAssemblies.ToArray())
.Where(x => x.GetInterfaces().Contains(typeof(IAutoMappedEntity)))))
另外,我没有手动设置的“MappingAssemblies”,我采取了只包含所有程序集的惰性方法,因此我的配置如下(使用SQLite,这是来自测试项目):
这是一个老问题,但在看了这个之后,我设法解决了它,所以我将尝试回答它。我就是这样做的(ForEach()是NHibernate.Linq的扩展): 我也必须为自动映射的东西做这个,那里的语法有点不同。我有一个接口,用于标记我要自动映射的所有类:
config.Mappings(m =>
m.AutoMappings.Add(AutoMap.Assemblies(MappingAssemblies.ToArray())
.Where(x => x.GetInterfaces().Contains(typeof(IAutoMappedEntity)))))
另外,我没有手动设置的“MappingAssemblies”,我采取了只包含所有程序集的惰性方法,因此我的配置如下(使用SQLite,这是来自测试项目):
奇怪的是,为什么要将与一个数据库相关的实体存储在不同的程序集中?并不是说这不是一个有效的问题,只是好奇而已。你使用的是FNH的哪个版本?我记得有一个关于多重映射的bug/限制-assemblies@Cole因此,成员和角色实体对象与静态数据库类来自同一程序集,但仍需要.AddAssemblyOf()调用。然后将其打包到.dll中,并在我决定制作的新解决方案中引用。除了调用.AddAssemblyOf()之外,为了将成员和角色对象映射到我的新解决方案数据库,我还需要映射此新解决方案中涉及的唯一实体对象;它将不在上述成员和角色对象中的同一程序集中。@Firo-如果我右键单击FlientNHibernate.dll并查看“产品版本”,其内容为:1.3.0.717很奇怪,但为什么要将与一个数据库相关的实体存储在不同的程序集中?并不是说这不是一个有效的问题,只是好奇而已。你使用的是FNH的哪个版本?我记得有一个关于多重映射的bug/限制-assemblies@Cole因此,成员和角色实体对象与静态数据库类来自同一程序集,但仍需要.AddAssemblyOf()调用。然后将其打包到.dll中,并在我决定制作的新解决方案中引用。除了调用.AddAssemblyOf()之外,为了将成员和角色对象映射到我的新解决方案数据库,我还需要映射此新解决方案中涉及的唯一实体对象;它将不在上述成员和角色对象的同一程序集中。@Firo-如果我右键单击FlientNHibernate.dll并查看“产品版本”,则显示为:1.3.0.717
var MappingAssemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => a.FullName.StartsWith("MyCompanyName."));
Configuration configuration;
var sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory())
// This adds fluent mappings
.Mappings(m => MappingAssemblies.ForEach(a => m.FluentMappings.AddFromAssembly(a)))
// This adds automapped classes using specific configuration
.Mappings(m => m.AutoMappings.Add(AutoMap.Assemblies(new MyAutomapConfiguration(), MappingAssemblies)))
// This adds automapped classes that just use my magic interface
.Mappings(m => m.AutoMappings.Add(AutoMap.Assemblies(MappingAssemblies.ToArray()).Where(x => x.GetInterfaces().Contains(typeof(IAutoMappedEntity)))))
.ExposeConfiguration(cfg => configuration = cfg)
.BuildSessionFactory();