Denna artikel är en spegelartikel om maskinöversättning, klicka här för att hoppa till originalartikeln.

Utsikt: 17312|Svar: 0

[Källa] Det finns tre konfigurationsmetoder för Nhibernate i .NET

[Kopiera länk]
Publicerad på 2016-08-08 13:17:51 | | |

1 App.config-konfiguration:

<?xml version="1.0" kodning="utf-8" ?>
<configuration>
<configSections>
    <sektionsnamn="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" requirePermission="false"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider,NHibernate</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <egendomsnamn="connection.connection_string">
        <!--Server=(lokal); Initial katalog=nhibernate; Integrerad säkerhet=SSPI-->
        databas='nhibernate'; server=.; uid='sa'; pwd='sa';
      </property>
      <egendomsnamn="adonet.batch_size">10</property>
      <egendomsnamn="show_sql">true</property>
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    </session-factory>
</hibernate-configuration>
----------------------------------------------------------------------------------------------------------------------

#region 2 。 App.config konfiguration
            Konfigurationskonfiguration = ny konfiguration(). AddAssembly("Test.Model");        

        fabrik = konfiguration. BuildSessionFactory();
          Session = fabrik. OpenSession();
#endregion

--------------------------------------------------------------------------------------------------------------------------
</configuration>

2 Web.config-konfiguration Notera bytepositionen som ska placeras

<!--NHibernate -->
   <sektionsnamn="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
   <!--NHibernate-->
</configSections>
<!--NHibernate-->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
   <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProviderProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <egendomsnamn="connection.connection_string">databas='nhibernate'; server=.; uid='sa'; pwd='sa'; </property>
    <egendomsnamn="show_sql">true</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
   </session-factory>
</hibernate-configuration>
<!--NHibernate-->
<appSettings/>

---------------------------------------------------------------------------------------------------------



#region 2. Använd web.config för att konfigurera

        NHibernate.Cfg.Configuration config = ny NHibernate.Cfg.Configuration(). AddAssembly("Test.Model");
        fabrik = konfiguration. BuildSessionFactory();
        Session = fabrik. OpenSession();

        #endregion

-----------------------------------------------------------------------------------------------------------

3 NHibernate.cfg.xml Konfiguration:

<?xml version="1.0" kodning="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-mapping-2.2">
<session-factory xmlns="urn:nhibernate-configuration-2.2">
    <!-- egenskaper -->
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProviderProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <egendomsnamn="connection.connection_string">databas='nhibernate'; server=.; uid='sa'; pwd='sa'; </property>
    <egendomsnamn="show_sql">true</property>
     <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    <egendomsnamn="use_outer_join">true</property>
    <!-- mappfiler -->
    <mapping assembly="Test.Model" />
</session-factory>
</hibernate-configuration>

-------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------

   #region 3. Använd NHibernate.cfg.xml konfiguration

        NHibernate.Cfg.Configuration config = ny NHibernate.Cfg.Configuration();
        konfiguration. Konfigure(@"C:\Documents and Settings\Administrator\Desktop\Text\NHibernateTest\WebSite1\NHibernate.cfg.xml");
        fabrik = konfiguration. BuildSessionFactory();
        Session = fabrik. OpenSession();
        #endregion



4 Skapa en MyConfiguration.cs struktur enligt följande:



public static NHibernate.Cfg.Configuration GetConfig()
    {

      
        NHibernate.Cfg.Configuration config = ny NHibernate.Cfg.Configuration();
        konfiguration. SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
        konfiguration. SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
        konfiguration. SetProperty("hibernate.connection.connection_string", "database='nhibernate'; server=.; uid='sa'; pwd='sa'; ");
        konfiguration. SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect,NHibernate");
        konfiguration. SetProperty("hibernate.use_outer_join", "true");
        konfiguration. SetProperty("hibernate.show_sql", "false");
        konfiguration. SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
        konfiguration. AddAssembly("Test.Model");
        returnera konfiguration;
      
    }



------------------------------------------------------------------------------------------------------------------------------

#region 4. Använd MyConfiguration.cs konfiguration

        NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig(); Använd MyConfiguration.cs
        fabrik = konfiguration. BuildSessionFactory();
        factory = MyConfiguration.GetConfig(). BuildSessionFactory();
        Session = fabrik. OpenSession();
        #endregion




Föregående:webapi:ObjectContent1类型未能序列化内容类型“application/xml; charset=ut...
Nästa:Kan inte läsa konfigurationsavsnittet "XXX" eftersom sektionssatsen saknas
Friskrivning:
All programvara, programmeringsmaterial eller artiklar som publiceras av Code Farmer Network är endast för lärande- och forskningsändamål; Ovanstående innehåll får inte användas för kommersiella eller olagliga ändamål, annars kommer användarna att bära alla konsekvenser. Informationen på denna sida kommer från internet, och upphovsrättstvister har inget med denna sida att göra. Du måste helt radera ovanstående innehåll från din dator inom 24 timmar efter nedladdning. Om du gillar programmet, vänligen stöd äkta programvara, köp registrering och få bättre äkta tjänster. Om det finns något intrång, vänligen kontakta oss via e-post.

Mail To:help@itsvse.com