开发者

Need to provide nhibernate configuration a path to an assembly

开发者 https://www.devze.com 2022-12-09 23:06 出处:网络
I have a solution that uses NHibernate to generate the db schema based on the mapping files. I\'m开发者_如何学JAVA trying to break that functionality out of the solution so it can be used as a stand a

I have a solution that uses NHibernate to generate the db schema based on the mapping files. I'm开发者_如何学JAVA trying to break that functionality out of the solution so it can be used as a stand alone console app. I was able to provide a path to the mapping files like so:

        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 gets to the point where it tries to add the file it complains that it can't find the NHibernateTestMappings.Core assembly because there is no reference to the assembly in my standalone app, but each mapping file contains a reference to the assembly:

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

What I need is a way to provide the nhibernate configuration a file path to my assembly's dll rather than adding a reference to it so that I can just swap out paths in an app.config and have this generate my schema.


Ok, I have this working now, however I may have uncovered a bug in nhibernate. Here's 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 nice enough to go find the path, he just takes the name and tries to look for it. Since that dll is not in the same directory as the app, it can't find it. My current solution will be to go out and grab the dll's and move them to my app directory, then remove them after the schema is generated. If anyone has any further suggestions I'm open to them.


Did you try this: ?

var myAssembly = System.Reflection.Assembly.LoadFrom(path);
0

精彩评论

暂无评论...
验证码 换一张
取 消