教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 NHibernate.cfg.xml文件配置示例

NHibernate.cfg.xml文件配置示例

发布时间:2016-11-28   编辑:jiaochengji.com
教程集为您提供NHibernate.cfg.xml文件配置示例等资源,欢迎您收藏本站,我们将为您提供最新的NHibernate.cfg.xml文件配置示例资源
NHibernate是一个面向.NET环境的对象/关系数据库映射工具。对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。本文我们来看看NHibernate.cfg.xml文件配置。

NHibernate.cfg.xml文件配置

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
     Data Source=.;database=NHibernateSample; uid=sa;pwd=123;
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true1, false0, yes 'Y', no 'N'</property>
<mapping assembly="DomainModel"/>
</session-factory>
</hibernate-configuration>
//创建数据库
publicclass DataBase
   {
privatevoid CreateDatabase()
       {
           Configuration cfg =new Configuration();
           cfg.Configure();
           SchemaExport schema =new SchemaExport(cfg);
//schema.Drop(true, false);
           schema.SetOutputFile("log.txt");
           schema.Create(true, false);
        }
    }




Nhibernate 另外几种配置方式

1 App.config 的配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
    <section name="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>
      <property name="connection.connection_string">
        <!--Server=(local);initial catalog=nhibernate;Integrated Security=SSPI-->
        database='nhibernate';server=.;uid='sa';pwd='sa';
      </property>
      <property name="adonet.batch_size">10</property>
      <property name="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 配置
            Configuration config = new Configuration().AddAssembly("Test.Model");        

        factory = config.BuildSessionFactory();
          session = factory.OpenSession();
#endregion

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

2 web.config的配置 注意 所放字节位置

<!--NHibernate -->
   <section name="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.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.connection_string">database='nhibernate';server=.;uid='sa';pwd='sa';</property>
    <property name="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、利用web.config 配置 通过

        //NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration().AddAssembly("Test.Model");
        //factory = config.BuildSessionFactory();
        //session = factory.OpenSession();

        #endregion

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

3     创建一个MyConfiguration.cs 结构如下:

 

public static NHibernate.Cfg.Configuration GetConfig()
    {

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

 

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

#region 3、使用MyConfiguration.cs配置

        //NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig();//使用MyConfiguration.cs
        //factory = config.BuildSessionFactory();
        factory = MyConfiguration.GetConfig().BuildSessionFactory();
        session = factory.OpenSession();
        #endregion

您可能感兴趣的文章:
NHibernate.cfg.xml文件配置示例
C#读写xml文件的简单例子
PHP配置文件应用(配置pdo与虚拟主机)
分享:shell去掉linux配置文件的注释行
asp.net中web.config文件详解
js读取配置文件示例代码
JQuery筛选器全系列介绍
更改php.ini配置文件示例
PHP怎么找到php.ini配置文件?
phpMyAdmin错误信息配置文件现在需要绝密的短语密码(blowfish_secret)

[关闭]
~ ~