views:

531

answers:

2

I have a solution that uses NHibernate to generate the db schema based on the mapping files. I'm 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.

+1  A: 

Did you try this: ?

var myAssembly = System.Reflection.Assembly.LoadFrom(path);
Mohamed Meligy
Yes, tried various things after posting this, none of which has worked. I updated my code to show my latest rendition. I also tried some appdomain stuff, but that failed because there is no default entry point as my core is a dll and not an exe.
Justin Holbrook
That's why I'm recommending that you use the code above.Then you'd call:cfg.AddAssembly(muAssembly);instead of the overload that takes a string, you use this oerload:public Configuration AddAssembly(Assembly assembly)Which accepts an "Assembly" object directly, and you decide how to get this assembly for it. Having a class library assemebly without entry point shouldn't be a problem at all.
Mohamed Meligy
+1  A: 

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.

Justin Holbrook