views:

26

answers:

2

I've got an MVC web-project that's referencing a class-library-project that contains my Domain and Persistence tiers. I'm using nHibernate for my OR/M. Since I've got multiple databases to connect to, I'm initializing my session-factories by specifying the file-names:

var config = new Configuration().Configure(nHibernateCfgFileName)

THE ISSUE: When I try to debug my MVC project, I get an error saying that it's not able to find the nHibernate config files (the files are configured to "Copy Always" to output directories). I've tried:

var x = Assembly.GetExecutingAssembly().Location;
var y = Assembly.GetEntryAssembly().Location;
var z = Assembly.GetCallingAssembly().Location;

But all of the above return back "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files....", and I've verified that the config files are not copied over into those temp locations (but they are in the bin\Debug location). Can someone please help me get to my config files?

A: 

Not exactly a solution to your problem, but I used this pattern for multiple databases using NHibernate, http://codebetter.com/blogs/karlseguin/archive/2009/03/30/using-nhibernate-with-multiple-databases.aspx.

I loaded my connection strings from configuration file rather than from the database by replacing this line:

var dataBases = _globalSessionFactory.OpenSession().CreateQuery("from DataBases").List<DataBases>();

with this:

var dataBases = GetDatabaseList();

private static IList<DataBase> GetDatabaseList() {
  var databases = new List<DataBase>();
  int numberOfDatabases = ConfigurationManager.AppSettings["NumberOfDatabases"].ToInt();
  for(int i = 1; i <= numberOfDatabases; i++)
    databases.Add(new DataBase
                    {
                      Identifier = ConfigurationManager.AppSettings["DatabaseName" + i],
                      ConnectionString = ConfigurationManager.AppSettings["ConnectionString" + i]
                    });
  return databases;
}
Luke Hutton
A: 

Ahh, changing .Location to .CodeBase seemed to do the trick:

var directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
Boris