【发布时间】:2011-12-03 23:44:24
【问题描述】:
考虑以下 Fluent 配置;
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);
所以我的想法是我已经将我的 Member 和 Role 实体对象打包到与 Database helper 对象相同的程序集中,这样我关心创建的任何解决方案都可以立即获得我的标准化成员资格,并且能够简单地创建它的拥有解决方案特定的 ClassMap 并将它们添加到配置对象中。
问题是,熟悉的调用;
config.Mappings(m =>
m.FluentMappings.AddFromAssembly(mappingAssembly)
);
似乎只能处理单个程序集。添加到列表中的内容无关紧要,只会映射添加的最后一个程序集。作为上述方法的替代方法,我尝试引用 MappingConfiguration (这是 config.Mappings(m => ) 中的 'm' 代表),但这也不起作用。很明显,对m.FluentMappings.AddFromAssembly 或任何FluentMappings.Add 方法的调用将覆盖以前存在的内容,但肯定有办法做到这一点吗?这似乎不是一个“奇怪”的要求。
【问题讨论】:
-
只是好奇,但为什么要将与一个数据库相关的实体存储在不同的程序集中?并不是说这不是一个有效的问题,而是再次好奇。
-
您使用哪个版本的 FNH?我记得有一个关于多个映射程序集的错误/限制
-
@Cole W. 因此,成员和角色实体对象来自与静态数据库类相同的程序集,但仍需要 .AddAssemblyOf
() 调用。然后将其打包成 .dll 并在我决定制作的新解决方案中引用。除了对 .AddAssemblyOf () 的调用之外,为了将成员和角色对象映射到我的新解决方案数据库,我还需要映射这个新解决方案中涉及的唯一实体对象;在上述 Member 和 Role 对象中,它们不会在同一个程序集中。 -
@Firo - 如果我右键单击 FlientNHibernate.dll 并查看“产品版本”,则显示为:1.3.0.717
标签: nhibernate fluent-nhibernate mapping