2009-12-31 14 views

cevap

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# kodu:

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

yerine yapılandırma yöneticisinden değerini almanın el emeği yapmanın, 'connection_string_name' ayarlamayı deneyin. Bkz. [ConnectionStrings> yapılandırma bölümünden bağlantı dizesi kullanmak için NHibernate nasıl yapılandırılır] (http://stackoverflow.com/questions/455664/how-to-configure-nhibernate-to-use-connection-string-from-connectionstrings- co) ve @ LachlanRoche'nin cevabı. –

+2

@Joel: Beni indirmeden önce soruyu okudun mu? Özellikle System.Configuration.ConfigurationManager çağırır. Lachlan'ın cevabı faydalıdır, ama sorulan soruya cevap vermez. –

+0

NHibernate'in "ConfigurationManager" da kullandığını düşünürsek, aynı işlevselliği yeniden yazmak için çok (çapraz proje) DRY değil. menüsünde –

22

hazırda yapılandırması da başlatma kodunu basitleştiren app.config, içine taşınabilir. NHibernate referans kılavuzunda bölüm XML Configuration File'a bakın.

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

Ve 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" çok önemlidir. Anlamadım ve hata yapmaya devam ettim. Teşekkür ederiz @Lachlan_Roche – Brij

İlgili konular