NHibernate的灵活配置

注:本文中使用的是NH1.0.2.0版本,其他版本配置可能略有不同。

示例代码下载
(没看明白的看看这个或许可以明白些)

数据库脚本下载


NHibernate的灵活配置

1.Visual Studio中文件的生成操作

在开始讨论这个话题前,我们想来看看Visual Studio中文件的生成操作。

无(None) - 不在项目输出组中包含该文件,并且在生成进程中不会对其进行编译。例如包含文档的文本文件,如自述文件。

编译(Compile) - 将该文件编译到生成输出中。此设置用于代码文件。

内容(Content) - 不编译该文件,但将其包含在“内容”(Content) 输出组中。例如,此设置是 .htm 或其他类型 Web 文件的默认值。

嵌入的资源(Embedded Resource) - 将该文件作为 DLL 或可执行文件嵌入主项目生成输出中。此设置通常用于资源文件。

 

对于嵌入的资源,我们可以启动“Visual Studio 2005 命令提示”,输入ildasm命令,使用启动的工具打开程序集文件,然后在MANIFEST中找到资源文件的信息,如下图。可以看见有名为“DDLLY.MyDoc.NHibernateTest.ConfigurationTest.hibernate1.cfg.xml”的资源文件。


 

2.Configuration配置

配置Configuration需要调用Configuration.Configure,我们来看看Configuration.Configure方法的信息

public Configuration Configure();

如果存在应用程序配置文件,则使用应用程序配置文件中的<hibernate-configuration>节点配置NHibernate,否则使用hibernate.cfg.xml进行配置。

public Configuration Configure(Assembly,string);
 
使用程序集中的资源配置NHibernate。
  public Configuration Configure(string)
 
c.使用指定的文件配置NHibernate。
  public Configuration Configure(XmlTextReader)
 
使用指定的XmlTextReader配置NHibernate。

 

我们可以使用多种方式进行配置,比较常见的配置有以下几种

a.可以在应用程序配置文件(对于普通程序而言为App.config,对Asp.net是Web.config)中进行配置,调用Configure()配置,配置文件如下  
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< configuration >
None.gif  
< configSections >
None.gif    
< section  name ="nhibernate"  type ="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"   />
None.gif  
</ configSections >
None.gif
None.gif  
< nhibernate >
None.gif    
< add 
None.gif            
key ="hibernate.show_sql"
None.gif            value
="true"
None.gif        
/>
None.gif    
< add 
None.gif            
key ="hibernate.connection.provider"           
None.gif            value
="NHibernate.Connection.DriverConnectionProvider"  
None.gif        
/>
None.gif        
< add 
None.gif            
key ="hibernate.dialect"                       
None.gif            value
="NHibernate.Dialect.MsSql2000Dialect"  
None.gif        
/>
None.gif        
< add 
None.gif            
key ="hibernate.connection.driver_class"           
None.gif            value
="NHibernate.Driver.SqlClientDriver"  
None.gif        
/>
None.gif        
< add 
None.gif            
key ="hibernate.connection.connection_string"  
None.gif            value
="server=.;database=user;uid=sa;pwd=App1234;"  
None.gif        
/>
None.gif
None.gif  
</ nhibernate >
None.gif
None.gif
</ configuration >
None.gif
None.gif
None.gif
 
 

这种配置方式只能对属性进行配置,不能配置映射类,而且应用程序配置文件可能还需要配置其他的信息,容易造成文件过于大,不方面浏览。所以我不推荐这种配置方式。

b.单独使用配置文件进行配置
 
b1.把配置文件放在程序应用程序同级目录,使用Configure()方法,此时配置文件名应为“hibernate.cfg.xml”
 
b2.把配置文件放到某个指定的路径,调用Configure(string)方法
 
b3.把配置文件作为资源嵌入到程序集(把文件的生成类型设置为"嵌入的资源"),调用Configure(Assembly,string)方法
 
 
这是一个典型配置文件
 
 
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
<!-- 普通配置文件,使用程序集加载映射文件 -->
None.gif
< hibernate-configuration  xmlns ="urn:nhibernate-configuration-2.0" >
None.gif    
< session-factory  name ="DDLLY.MyDoc.NHibernate.QuickStart" >
None.gif        
<!--  属性  -->
None.gif        
< property  name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property >
None.gif        
< property  name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property >
None.gif        
< property  name ="connection.connection_string" > server=.;database=user;uid=sa;pwd=App1234; </ property >
None.gif        
< property  name ="show_sql" > false </ property >
None.gif        
< property  name ="dialect" > NHibernate.Dialect.MsSql2000Dialect </ property >
None.gif        
< property  name ="use_outer_join" > true </ property >
None.gif        
< property  name ="query.substitutions" > true 1, false 0, yes 'Y', no 'N' </ property >
None.gif        
<!--  映射文件  -->
None.gif        
<!-- 程序将加载DDLLY.MyDoc.NHibernateTest.Configuration命名空间所有的映射文件(*.hbm.xml) -->
None.gif        
< mapping  assembly ="DDLLY.MyDoc.NHibernateTest.ConfigurationTest"   />
None.gif    
</ session-factory >
None.gif
</ hibernate-configuration >
None.gif

我写了个类来测试
 
 
None.gif using  System.Reflection;
None.gif
using  NHibernate.Cfg;
None.gif
using  NUnit.Framework;
None.gif
None.gif
namespace  DDLLY.MyDoc.NHibernateTest.ConfigurationTest.Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 测试ConfigurationTest.Configure()方法的测试类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TestFixture]
InBlock.gif    
public class ConfigureFixture
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
测试方法#region 测试方法
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试ConfigurationTest.Configure()方法 
InBlock.gif        
/// 加载和应用程序同目录的hibernate.cfg.xml
InBlock.gif        
/// 将加载"bin/Debug或Release下的hibernate.cfg.xml"
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigure1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Configuration cfg 
= new Configuration().Configure();
InBlock.gif            Assert.AreEqual(
"true 1, false 0, yes 'Y', no 'N'", cfg.Properties[Environment.QuerySubstitutions]);
InBlock.gif            Assert.AreEqual(
"server=.;database=user;uid=sa;pwd=App1234;", cfg.Properties[Environment.ConnectionString]);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试Configure(Assembly,string)方法,加载嵌入在程序集中的hibernate.cfg.xml
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigure2()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Configuration cfg 
= new Configuration().Configure(
InBlock.gif                Assembly.GetExecutingAssembly(), 
//当前代码正从中运行的程序集。
InBlock.gif
                TestHelper.NAMEPLACENAME + ".hibernate.cfg.xml");
InBlock.gif            Assert.AreEqual(
"true 1, false 0, yes 'Y', no 'N'", cfg.Properties[Environment.QuerySubstitutions]);
InBlock.gif            Assert.AreEqual(
"server=.;database=user;uid=sa;pwd=App1234;", cfg.Properties[Environment.ConnectionString]);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试Configure(string)方法,加载指定路径的文件
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigure3()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Configuration cfg 
= new Configuration().Configure("hibernate.cfg.xml");
InBlock.gif            Assert.AreEqual(
"true 1, false 0, yes 'Y', no 'N'", cfg.Properties[Environment.QuerySubstitutions]);
InBlock.gif            Assert.AreEqual(
"server=.;database=user;uid=sa;pwd=App1234;", cfg.Properties[Environment.ConnectionString]);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

 让我们来看看配置文件,配置文件中主要配置的是各种属性和映射类的信息(还有缓存配置方面的信息,将在后面的章节讲到)。

在NHibernate.Cfg.Environment类中可以看到的可用属性信息。
例如Environment中public const string ConnectionString = "hibernate.connection.connection_string";

关于属性的说明可以查看NHibernate的文档。

映射文件的配置有以下几种方法

a.使用程序集中的所有资源文件

 
 
None.gif <!-- 程序将加载DDLLY.MyDoc.NHibernateTest.Configuration命名空间所有的映射文件(*.hbm.xml) -->
None.gif
< mapping  assembly ="DDLLY.MyDoc.NHibernateTest.ConfigurationTest"   />
None.gif

b.使用指定文件

 
 
None.gif <!--  映射文件  -->
None.gif
<!-- 程序将加载指定的文件 -->
None.gif
<!-- 这种写法将加载和可执行程序的路径中的User.hbm.xml文件 -->
None.gif
<!-- 在Visual Studio把生成的可执行文件放在bin\Debug和bin\Release中 -->
None.gif
<!-- 所以此文件也要放在这两个目录中 -->
None.gif
< mapping  file ="User.hbm.xml"   />
None.gif

c.使用程序集中的指定资源文件

 
 
None.gif <!--  映射文件  -->
None.gif
<!-- 程序将加载指定的文件 -->
None.gif
<!-- 这种写法将加载加载资源文件 -->
None.gif
< mapping  resource ="DDLLY.MyDoc.NHibernateTest.ConfigurationTest.User.hbm.xml"  assembly ="DDLLY.MyDoc.NHibernateTest.ConfigurationTest"   />
None.gif
None.gif 
None.gif

我们也可以使用代码来配置这些内容。

AddAssembly

添加程序集中所有以"hbm.xml"为结尾的文件作为映射文件。

示例:cfg.AddAssembly(Assembly.GetExecutingAssembly());

AddClass

使用协定从应用程序资源读取一个映射。类 Foo.Bar.Foo被映射为名为Foo.Bar.Foo.hbm.xml的嵌入在类所在的程序集中的资源文件。

示例:cfg.AddClass(typeof (User));

AddFile

从一个特别的XML文件读取映射。这个方法和AddXmlFile作用一样。

示例:cfg.AddFile("User.hbm.xml");

AddProperties

添加一个IDictionary类型的属性集合。Key是属性名,Value是属性的值。

示例:

//添加连接字符串属性
Hashtable properties = new Hashtable();
properties.Add(Environment.ConnectionString, //NHibernate.Cfg.Environment类中有所有的可属性信息
"server=.;database=user;uid=sa;pwd=App1234;");
cfg.AddProperties(properties);

我比较喜欢使用配置文件里面配置程序集的做法,因为这种做法最简单直接。

我们也可以把两种方式结合起来使用,例如我们可以在配置文件中配置好除连接字符串外的其它信息,然后在代码中添加连接字符串。

不管是映射信息还是属性信息最终都回被设置到Configuration里,我们可以查看Configuration的ClassMappings和Properties来查看这些信息。

同样我也写了两个类,前一个用来测试配置映射类。后一个用来测试配置属性。

测试配置映射

None.gif using  System.Reflection;
None.gif
using  NHibernate.Cfg;
None.gif
using  NUnit.Framework;
None.gif
None.gif
namespace  DDLLY.MyDoc.NHibernateTest.ConfigurationTest.Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 测试加载映射文件的类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TestFixture]
InBlock.gif    
public class ConfigMappingFixture
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
SetUp和TearDown#region SetUp和TearDown
InBlock.gif
InBlock.gif        [SetUp]
InBlock.gif        
public void SetUp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//添加名为LLY,密码为123456的用户,因为这是数据库的第一条记录,所以数据库标识为1
InBlock.gif
            string SetUpSql = "INSERT INTO users([UserName],[Password]) VALUES('LLY','123456')";
InBlock.gif            TestHelper.SqlExecuteNonQuery(SetUpSql);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [TearDown]
InBlock.gif        
public void TearDown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//清空users表,并把标识(identity)清零
InBlock.gif
            string TearDownSql = "TRUNCATE TABLE users";
InBlock.gif            TestHelper.SqlExecuteNonQuery(TearDownSql);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
使用配置文件配置映射的测试#region 使用配置文件配置映射的测试
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试使用配置文件中配置的程序集加载映射文件
InBlock.gif        
/// 
InBlock.gif        
/// <!-- 映射文件 -->
InBlock.gif        
///<!-- 程序将加载DDLLY.MyDoc.NHibernateTest.Configuration命名空间所有的映射文件(*.hbm.xml)-->
InBlock.gif        
///<mapping assembly="DDLLY.MyDoc.NHibernateTest.ConfigurationTest" />
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigMappingUseFile1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//配置Configuration
InBlock.gif
            Configuration cfg = new Configuration().Configure(
InBlock.gif                Assembly.GetExecutingAssembly(), 
//当前代码正从中运行的程序集。
InBlock.gif
                TestHelper.NAMEPLACENAME + ".hibernate.cfg.xml");
InBlock.gif
InBlock.gif            
//测试
InBlock.gif
            TestHelper.CheckConfiguration(cfg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试以文件形式加载映射文件,使用配置文件中的配置
InBlock.gif        
/// 
InBlock.gif        
///<!-- 映射文件 -->
InBlock.gif        
///<!--程序将加载指定的文件-->
InBlock.gif        
///<!--这种写法将加载和可执行程序的路径中的User.hbm.xml文件-->
InBlock.gif        
///<!--在Visual Studio把生成的可执行文件放在bin\Debug和bin\Release中-->
InBlock.gif        
///<!--所以此文件也要放在这两个目录中-->
InBlock.gif        
///<mapping file="User.hbm.xml" />
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigMappingUseFile2()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//配置Configuration
InBlock.gif
            Configuration cfg = new Configuration().Configure(
InBlock.gif                Assembly.GetExecutingAssembly(), 
//当前代码正从中运行的程序集。
InBlock.gif
                TestHelper.NAMEPLACENAME + ".hibernate1.cfg.xml");
InBlock.gif
InBlock.gif            
//测试
InBlock.gif
            TestHelper.CheckConfiguration(cfg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试加载资源文件作为映射文件,使用配置文件中的配置
InBlock.gif        
/// 
InBlock.gif        
///<!-- 映射文件 -->
InBlock.gif        
///<!--程序将加载指定的文件-->
InBlock.gif        
///<!--这种写法将加载加载资源文件-->
InBlock.gif        
///<mapping resource="DDLLY.MyDoc.NHibernateTest.ConfigurationTest.User.hbm.xml" assembly="DDLLY.MyDoc.NHibernateTest.ConfigurationTest" />
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigMappingUseFile3()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//配置Configuration
InBlock.gif
            Configuration cfg = new Configuration().Configure(
InBlock.gif                Assembly.GetExecutingAssembly(), 
//当前代码正从中运行的程序集。
InBlock.gif
                TestHelper.NAMEPLACENAME + ".hibernate2.cfg.xml");
InBlock.gif
InBlock.gif            
//测试
InBlock.gif
            TestHelper.CheckConfiguration(cfg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
使用程序代码配置映射的测试#region 使用程序代码配置映射的测试
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试在代码中加载映射文件,使用程序集
InBlock.gif        
/// hibernate3.cfg.xml中未设置映射类信息使用代码添加
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigMappingUseCode1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//配置Configuration
InBlock.gif
            Configuration cfg = new Configuration().Configure(
InBlock.gif                Assembly.GetExecutingAssembly(), 
//当前代码正从中运行的程序集。
InBlock.gif
                TestHelper.NAMEPLACENAME + ".hibernate3.cfg.xml");
InBlock.gif
InBlock.gif            
//加载程序集中所有映射文件
InBlock.gif
            cfg.AddAssembly(Assembly.GetExecutingAssembly());
InBlock.gif
InBlock.gif            
//测试
InBlock.gif
            TestHelper.CheckConfiguration(cfg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试在代码中加载映射文件,使用类
InBlock.gif        
/// hibernate3.cfg.xml中未设置映射类信息使用代码添加
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigMappingUseCode2()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//配置Configuration
InBlock.gif
            Configuration cfg = new Configuration().Configure(
InBlock.gif                Assembly.GetExecutingAssembly(), 
//当前代码正从中运行的程序集。
InBlock.gif
                TestHelper.NAMEPLACENAME + ".hibernate3.cfg.xml");
InBlock.gif
InBlock.gif            
//加载和类对应的映射文件
InBlock.gif
            cfg.AddClass(typeof (User));
InBlock.gif
InBlock.gif            
//测试
InBlock.gif
            TestHelper.CheckConfiguration(cfg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试在代码中加载映射文件,使用文件
InBlock.gif        
/// hibernate3.cfg.xml中未设置映射类信息使用代码添加
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigMappingUseCode3()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//配置Configuration
InBlock.gif
            Configuration cfg = new Configuration().Configure(
InBlock.gif                Assembly.GetExecutingAssembly(), 
//当前代码正从中运行的程序集。
InBlock.gif
                TestHelper.NAMEPLACENAME + ".hibernate3.cfg.xml");
InBlock.gif
InBlock.gif            
//加载和类对应的映射文件
InBlock.gif
            cfg.AddFile("User.hbm.xml");
InBlock.gif
InBlock.gif            
//测试
InBlock.gif
            TestHelper.CheckConfiguration(cfg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

 

测试配置属性

 
 
None.gif using  System.Collections;
None.gif
using  System.Reflection;
None.gif
using  NHibernate.Cfg;
None.gif
using  NUnit.Framework;
None.gif
None.gif
namespace  DDLLY.MyDoc.NHibernateTest.ConfigurationTest.Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 测试配置属性的类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TestFixture]
InBlock.gif    
public class ConfigPropertyFixture
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
SetUp和TearDown#region SetUp和TearDown
InBlock.gif
InBlock.gif        [SetUp]
InBlock.gif        
public void SetUp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//添加名为LLY,密码为123456的用户,因为这是数据库的第一条记录,所以数据库标识为1
InBlock.gif
            string SetUpSql = "INSERT INTO users([UserName],[Password]) VALUES('LLY','123456')";
InBlock.gif            TestHelper.SqlExecuteNonQuery(SetUpSql);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [TearDown]
InBlock.gif        
public void TearDown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//清空users表,并把标识(identity)清零
InBlock.gif
            string TearDownSql = "TRUNCATE TABLE users";
InBlock.gif            TestHelper.SqlExecuteNonQuery(TearDownSql);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
测试方法#region 测试方法
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试在代码中配置属性
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestConfigProperty()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//配置Configuration
InBlock.gif
            Configuration cfg = new Configuration().Configure(
InBlock.gif                Assembly.GetExecutingAssembly(), 
//当前代码正从中运行的程序集。
InBlock.gif
                TestHelper.NAMEPLACENAME + ".hibernate4.cfg.xml");
InBlock.gif
InBlock.gif            
//hibernate4.cfg.xml未配置连接字符串属性,使用代码添加连接字符串属性
InBlock.gif
            Hashtable properties = new Hashtable();
InBlock.gif            properties.Add(Environment.ConnectionString, 
//NHibernate.Cfg.Environment类中有所有的可属性信息
InBlock.gif
                           "server=.;database=user;uid=sa;pwd=App1234;");
InBlock.gif            cfg.AddProperties(properties);
InBlock.gif
InBlock.gif            
//测试
InBlock.gif
            TestHelper.CheckConfiguration(cfg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/renrenqq/archive/2006/08/02/466056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值