2009-12-31 15 views

Odpowiedz

18

app.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <connectionStrings> 
    <add name="Northwind" connectionString= 
     "Data Source=(local);Initial Catalog=Northwind;Trusted_Connection=True;> 
    </connectionStrings> 
</configuration> 

C#:

string connectionString = System.Configuration.ConfigurationManager 
           .ConnectionStrings["Northwind"].ToString(); 

NHibernate.Cfg.Configuration nHibernateConfiguration = 
             new NHibernate.Cfg.Configuration(); 
nHibernateConfiguration.SetProperty(
    NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, 
    typeof(NHibernate.ByteCode.Castle.ProxyFactoryFactory).AssemblyQualifiedName); 
nHibernateConfiguration.SetProperty(
    NHibernate.Cfg.Environment.Dialect, 
    typeof(NHibernate.Dialect.MsSql2005Dialect).AssemblyQualifiedName); 
nHibernateConfiguration.SetProperty(
    NHibernate.Cfg.Environment.ConnectionString, connectionString); 
nHibernateConfiguration.SetProperty(
    NHibernate.Cfg.Environment.FormatSql, "true"); 
nHibernateConfiguration.AddAssembly(Assembly.GetCallingAssembly()); 

ISessionFactory oneISessionFactory = nHibernateConfiguration 
             .BuildSessionFactory(); 
+0

Zamiast robić pracy ręcznej na uzyskanie wartości z menedżera konfiguracji, spróbuj ustawić 'connection_string_name'. Zobacz [Jak skonfigurować NHibernate, aby użyć łańcucha połączenia z sekcji konfiguracji ] (http://stackoverflow.com/questions/455664/how-to-configure-nhibernate-to-use-connection-string-from-connection-- co) i odpowiedź @ LachlanRoche. –

+2

@Joel: Czy przeczytałeś pytanie zanim mnie ocuciłeś? W szczególności wywołuje System.Configuration.ConfigurationManager. Odpowiedź Lachlana jest pomocna, ale nie odpowiada na zadane pytanie. –

+0

Biorąc pod uwagę, że NHibernate również używa 'ConfigurationManager', nie jest to DRY (projekt krzyżowy), aby przepisać ten sam element funkcjonalności. –

22

Konfiguracja hibernacji może być przeniesiony do app.config, co ułatwia kodu startowego. Zobacz rozdział XML Configuration File w podręczniku referencyjnym NHibernate.

Configuration cfg = new NHibernate.Cfg.Configuration(); 
ISessionFactory sf = cfg.Configure().BuildSessionFactory(); 

A w app.config:

<configuration> 
     <configSections> 
      <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> 
     </configSections> 
     <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
      <session-factory> 
       <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
       <property name="connection.dialect">NHibernate.Dialect.MsSql2005Dialect</property> 
       <property name="connection.connection_string_name">Northwind</property> 
       <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> 
       <mapping assembly="assemblyname" /> 
      </session-factory> 
     </hibernate-configuration> 
     <connectionStrings> 
       <add name="Northwind" connectionString="Data Source=(local);Initial Catalog=Northwind;Trusted_Connection=True;> 
     </connectionStrings> 
</configuration> 
+0

xmlns = "urn: nhibernate-configuration-2.2" w jest bardzo ważne. Nie miałem go i ciągle dostaję błąd. Dziękuję @ Lachlan_Roche – Brij

Powiązane problemy