如何在MappingIntegrationTests中生成xml映射文件作为测试的一部分?
我需要手动检查fluent映射是否与leagcy项目中的映射相关。
发布于 2010-01-10 17:51:37
你可以这样做:
config.Mappings(m =>
{
m.FluentMappings.ExportTo("...file path here...");
m.HbmMappings.ExportTo("...file path here...");
m.AutoMappings.ExportTo("...file path here...");
{
);
我自己也不喜欢。如果我找到了更好的方法(如果有的话),我会更新答案。
看见
http://blog.jagregory.com/2009/02/03/fluent-nhibernate-configuring-your-application/
或者,如果坏了,请看下面这个
https://github.com/jagregory/fluent-nhibernate/wiki/Database-configuration
发布于 2010-01-10 17:47:57
您可以通过调用ExportTo()方法来生成XML映射。
例如:
ISessionFactory sessionFactory = FluentNHibernate.Cfg.Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
.ConnectionString(connectionString)
)
.Mappings(m => m.FluentMappings.AddFromAssembly(assembly)
.ExportTo(@"C:\your\export\path")
)
.BuildSessionFactory();
有关文档,请参阅此处:
发布于 2011-02-22 04:20:44
我使用(几乎)这个扩展方法在内存中获取xbm,这样我就可以在我的测试项目中查看它:
public static IDictionary<string, string> LoadHBM(this FluentConfiguration cfg)
{
var result = new Dictionary<string, string>();
var mem = new MemoryStream();
var writer = new StreamWriter(mem);
var reader = new StreamReader(mem);
cfg.Mappings(x =>
{
x.FluentMappings.ExportTo(writer);
x.AutoMappings.ExportTo(writer);
});
cfg.BuildConfiguration();
writer.Flush();
mem.Seek(0, 0);
var hbm = reader.ReadToEnd();
var objects = XElement.Parse("<junk>" + hbm + "</junk>").Elements();
objects.ToList().ForEach(x => result.Add(x.Elements().First().Attribute("name").Value, x.ToString()));
return result;
}
编辑:针对FNH 1.2进行了更新。
https://stackoverflow.com/questions/2036270
复制相似问题