Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Multiple databases in Fluent Nhibernate, System.TypeInitializationException was unhandled
    text
    copied!<p>I was trying to implement <a href="http://codebetter.com/karlseguin/2009/03/30/using-nhibernate-with-multiple-databases/" rel="nofollow noreferrer">Multiple DB connected Fluent NHibernate sample Application</a>.</p> <p>My Fluent NHibernate <a href="http://code.google.com/p/nhibernate-repository-example/" rel="nofollow noreferrer">Repository</a> is as follows:</p> <pre><code>namespace RepositoryExample.Repository.NHibernate { public class NHibernateRepository&lt;T&gt; : IRepository&lt;T&gt; where T : class { protected static Configuration config; private static readonly ISessionFactory _globalSessionFactory = new Configuration().Configure().BuildSessionFactory(); protected static IDictionary&lt;string, ISessionFactory&gt; _allFactories; public NHibernateRepository() { IDictionary&lt;string, string&gt; dataBases = new Dictionary&lt;string, string&gt;(); dataBases.Add("Monkey", @"Data Source=.\SQLEXPRESS;AttachDbFilename='MonkeyDB.mdf';Integrated Security=True;User Instance=True"); dataBases.Add("Banana", @"Data Source=.\SQLEXPRESS;AttachDbFilename='Banana.mdf';Integrated Security=True;User Instance=True"); // Error at below line (See stack trace for deatils below) _allFactories = new Dictionary&lt;string, ISessionFactory&gt;(dataBases.Count); foreach (var dataBase in dataBases) { config = Fluently.Configure() .Database( MsSqlConfiguration.MsSql2008.ConnectionString(dataBase.Value)) .Mappings(m =&gt; m.FluentMappings.AddFromAssemblyOf&lt;NHibernateRepository&lt;T&gt;&gt;()) .BuildConfiguration(); _allFactories.Add(dataBase.Key, config.BuildSessionFactory()); } } public ISession GetSession() { var currentIdentifier = GetCurrentIdentifier(); return _allFactories[currentIdentifier].OpenSession(); } private string GetCurrentIdentifier() { if (typeof(T).ToString().Contains("Banana")) { return "Banana"; } else { return "Monkey"; } } public void Save(T value) { var sessioned = GetSession(); using (var transaction = sessioned.BeginTransaction()) { sessioned.Save(value); transaction.Commit(); } } public void GenerateSchema(SanityCheck AreYouSure) { new SchemaExport(config).Create(true,true); } } } </code></pre> <p>And I'm calling as follows:</p> <pre><code>public class NHibernateIntegrationTest { static void Main(string[] args) { var repo = new NHibernateRepository&lt;NHibernateIntegrationTest&gt;(); repo.GenerateSchema(SanityCheck.ThisWillDropMyDatabase); } } </code></pre> <p>Error at:</p> <pre><code>_allFactories = new Dictionary&lt;string, ISessionFactory&gt;(dataBases.Count); </code></pre> <p>It is in NHibernateRepository class's default constructor.</p> <pre><code>StackTrace: System.TypeInitializationException was unhandled Message=The type initializer for 'RepositoryExample.Repository.NHibernate.NHibernateRepository`1' threw an exception. Source=RepositoryExample TypeName=RepositoryExample.Repository.NHibernate.NHibernateRepository`1 StackTrace: at RepositoryExample.Repository.NHibernate.NHibernateRepository`1..ctor() in C:\Documents and Settings\pavankumarn\My Documents\Downloads\nhibernate-repository-example\RepositoryExample\Repository\NHibernate\NHibernateRepository.cs:line 44 at Test.NHibernateIntegrationTest.Main(String[] args) in C:\Documents and Settings\pavankumarn\My Documents\Downloads\nhibernate-repository-example\Test\NHibernateIntegrationTest.cs:line 17 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: NHibernate.Cfg.HibernateConfigException Message=An exception occurred during configuration of persistence layer. Source=NHibernate StackTrace: at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting) at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader) at NHibernate.Cfg.Configuration.Configure(XmlReader textReader) at NHibernate.Cfg.Configuration.Configure(String fileName, Boolean ignoreSessionFactoryConfig) at NHibernate.Cfg.Configuration.Configure(String fileName) at NHibernate.Cfg.Configuration.Configure() at RepositoryExample.Repository.NHibernate.NHibernateRepository`1..cctor() in C:\Documents and Settings\pavankumarn\My Documents\Downloads\nhibernate-repository-example\RepositoryExample\Repository\NHibernate\NHibernateRepository.cs:line 17 InnerException: System.IO.FileNotFoundException Message=Could not find file 'C:\Documents and Settings\pavankumarn\My Documents\Downloads\nhibernate-repository-example\Test\bin\Debug\hibernate.cfg.xml'. Source=mscorlib FileName=C:\Documents and Settings\pavankumarn\My Documents\Downloads\nhibernate-repository-example\Test\bin\Debug\hibernate.cfg.xml StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver) at System.Threading.CompressedStack.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state) at System.Xml.XmlTextReaderImpl.OpenUrl() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlTextReader.Read() at System.Xml.XmlCharCheckingReader.Read() at System.Xml.XsdValidatingReader.Read() at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space) at System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space) at System.Xml.XPath.XPathDocument..ctor(XmlReader reader) at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting) InnerException: </code></pre> <p>I'm not using hibernate.cfg.xml, since I'm configuring the session factory Fluently. But Inner stack trace is showing that hibernate.cfg.xml file is not found. See the below screenshot. <img src="https://i.stack.imgur.com/Gi3b9.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload