我想使用连接字符串App.Config文件。也可以使用NHibernate连接到2个数据库(不同的连接字符串),如果是这样的话?
答案 0 :(得分:7)
问题的第一部分:
<property name="connection.connection_string_name">ConnStringName</property>
然后像往常一样宣布你的<connectionStrings>
。 @JMSA错了。
答案 1 :(得分:2)
如果保持2个不同的数据库连接,Frederik Gheysels的链接是最好的答案。
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提供了三种读取配置信息的方法:
从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();
}
答案 2 :(得分:2)
您可以创建多个ISessionFactories;每个要连接的数据库都有一个。
检查this article。
答案 3 :(得分:0)
可以使用Fluent NHibernate。您可以使用原始连接字符串,应用程序设置或从connectionStrings部分读取。从connectionStrings部分读取的示例是:
ISessionFactory factory = Fluently.Configure()
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyClass>())
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey("connectionStringKey"))
.BuildSessionFactory();
正如Frederik所提到的,您可以通过配置多个会话工厂来处理多个数据库。