我有一个解决方案,它使用 NHibernate 根据映射文件生成数据库模式。我正在尝试将该功能从解决方案中分离出来,以便它可以用作独立的控制台应用程序。我能够像这样提供映射文件的路径:
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
/**/
Assembly contractsAssembly = Assembly.LoadFrom(@"C:\Data\Development\NHibernateTestMappings\Source\DomainModel\Core\bin\Debug\NHibernateTestMappings.Core.Contracts.dll");
Assembly assembly = Assembly.LoadFrom(@"C:\Data\Development\NHibernateTestMappings\Source\DomainModel\Core\bin\Debug\NHibernateTestMappings.Core.dll");
cfg.AddAssembly(contractsAssembly);
cfg.AddAssembly(assembly);
/**/
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Data\Development\NHibernateTestMappings\Source\DomainModel\Core\Mappings");
FileInfo[] mappingfiles = directoryInfo.GetFiles("*.hbm.xml");
foreach (FileInfo fi in mappingfiles)
{
cfg.AddFile(fi.FullName);
//cfg.Configure(myAssembly, fi.FullName);
//cfg.AddResource(fi.FullName, myAssembly);
}
因此,当它到达尝试添加文件的位置时,它提示找不到 NHibernateTestMappings.Core 程序集,因为在我的独立应用程序中没有对该程序集的引用,但每个映射文件都包含一个引用大会:
<class name="NHibernateTestMappings.Core.Store, NHibernateTestMappings.Core" table="STORE" lazy="false">
我需要的是一种为 nhibernate 配置提供程序集 dll 文件路径的方法,而不是添加对它的引用,这样我就可以在 app.config 中换出路径并让它生成我的架构。
最佳答案
好的,我现在可以正常工作了,但是我可能发现了 nhibernate 中的一个错误。这是来自 NHibernate.cfg.Configuration.AddAssembly 的代码:
public Configuration AddAssembly( string assemblyName )
{
log.Info( "searching for mapped documents in assembly: " + assemblyName );
Assembly assembly = null;
try
{
assembly = Assembly.Load( assemblyName );
}
catch( Exception e )
{
log.Error( "Could not configure datastore from assembly", e );
throw new MappingException( "Could not add assembly named: " + assemblyName, e );
}
return this.AddAssembly( assembly );
}
所以 Assembly.Load(assemblyName) 不关心我是否足够好去寻找路径,他只是获取名称并尝试寻找它。由于该 dll 与应用程序不在同一目录中,因此无法找到它。我目前的解决方案是出去抓取 dll 并将它们移动到我的应用程序目录,然后在生成模式后删除它们。如果有人有任何进一步的建议,我愿意接受。
关于c# - 需要为 nhibernate 配置提供程序集的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1644113/