Must provide nhibernate configuration of assembly path

I have a solution that NHibernate uses to generate a db schema based on mapping files. I am trying to tear this functionality out of a solution so that it can be used as a standalone console application. I was able to specify the path to the mapping files as follows:

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); } 

So, when it comes to the point where he is trying to add a file, he complains that he cannot find the NHibernateTestMappings.Core assembly, because there is no assembly reference in my standalone application, but each mapping file contains an assembly reference:

 <class name="NHibernateTestMappings.Core.Store, NHibernateTestMappings.Core" table="STORE" lazy="false"> 

What I need is to provide the nhibernate configuration with the path to my dll assembly, and not add a link to it so that I can just change the paths in app.config and generate my circuit.

+4
source share
2 answers

Ok, now it works for me, however, maybe I found an error in nhibernate. Here is the code from 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 ); } 

So Assembly.Load (assemblyName) doesn't care that I was good enough to find the path, it just takes the name and tries to find it. Since this DLL is not in the same directory as the application, it cannot find it. My current solution would be to go out and grab the dlls and transfer them to the application directory, and then delete them after creating the schema. If anyone has any further suggestions, I am open to them.

+3
source

Have you tried this :?

 var myAssembly = System.Reflection.Assembly.LoadFrom(path); 
+1
source

All Articles