开发者

Unable to locate my nhibernate config file when debugging my MVC web-application

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\T开发者_Python百科emporary 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?


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


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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜