В случае поддержания двух разных соединений с БД, ссылка Фредерика Гейселя - лучший ответ.
AFAIK невозможно использовать стандартный формат App.config, например:
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ConnString1" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=NhibernateDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
NHibernate предоставляет 3 способа чтения информации о конфигурации:
Чтение конфигурации из файла App.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<!-- NHibernate Configuration -->
<nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.connection.connection_string" value="Data Source=(local)\sqlexpress;Initial Catalog=NHibernateDB;user=sa;Password=;Integrated Security=true"/>
</nhibernate>
</configuration>
Улучшен способ чтения конфигурации из файла App.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<!-- NHibernate Configuration -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=(local)\sqlexpress;Initial Catalog= NHibernateDB;user=sa;Password=;Integrated Security=True;</property>
<mapping assembly="NHibernate_Test.BO"/>
</session-factory>
</hibernate-configuration>
</configuration>
Чтение конфигурации из файла hibernate.cfg.xml
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=NHibernateDB; user=sa;Password=;Integrated Security=True</property>
<property name="show_sql">false</property>
<!--<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>-->
<mapping assembly="NHibernate_Test.BO" />
</session-factory>
</hibernate-configuration>
Я могу предложить вам загрузить информацию о конфигурации в коде так:
public sealed class Persister : System.IDisposable
{
private NHibernate.ISessionFactory _sessionFactory;
/// <summary> Configures NHibernate to access the data source and map entities to tables. </summary>
public Persister()
{
System.Console.Out.WriteLine("Configuration of NHibernate...\n");
const string connectionString = @"Data Source=(local)\sqlexpress;Initial Catalog=nhibernate;Integrated Security=SSPI";
// Enable the logging of NHibernate operations
log4net.Config.XmlConfigurator.Configure();
// Create the object that will hold the configuration settings
// and fill it with the information to access to the database
NHibernate.Cfg.Configuration configuration = new NHibernate.Cfg.Configuration();
configuration.Properties[NHibernate.Cfg.Environment.ConnectionProvider] = "NHibernate.Connection.DriverConnectionProvider";
System.Console.Out.WriteLine("Use SQL Server database: ConnectionString = <"
+ connectionString + ">\n");
// These are the three lines of code to change in order to use another database
configuration.Properties[NHibernate.Cfg.Environment.Dialect] = "NHibernate.Dialect.MsSql2000Dialect";
configuration.Properties[NHibernate.Cfg.Environment.ConnectionDriver] = "NHibernate.Driver.SqlClientDriver";
configuration.Properties[NHibernate.Cfg.Environment.ConnectionString] = connectionString;
// Use NHibernate.Mapping.Attributes to create mapping information about our entities
System.Console.Out.WriteLine("Generating the mapping information for NHibernate...\n");
NHibernate.Mapping.Attributes.HbmSerializer.Default.Validate = true; // Enable validation (optional)
using (System.IO.MemoryStream stream = NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(System.Reflection.Assembly.GetExecutingAssembly()))
{
configuration.AddInputStream(stream); // Send the mapping information to NHibernate configuration
}
// Create the table in the database for the entity Message
System.Console.Out.WriteLine("Creating the table in the database for the entity Message...");
new NHibernate.Tool.hbm2ddl.SchemaExport(configuration).Create(true, true);
// Build the SessionFactory
System.Console.Out.WriteLine("\n\nBuilding the session factory, end of the configuration\n\n");
_sessionFactory = configuration.BuildSessionFactory();
}
public void Dispose()
{
// Do not forget to close the session factory when you are done with it
_sessionFactory.Close();
}