16. 运行程序,结果如下图所示:

17. 一般情况下,我们并不想将连接字符串定义在代码中,而是放在配置文件中。我们可以在项目中添加一个App.config文件,添加如下代码:
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="Sample" connectionString="server=.;database=CodeConfigurationSample;integrated security=SSPI;"/> </connectionStrings> </configuration>
18. 在GetConfiguration方法中,修改连接字符串如下:
cfg.Properties.Add(NHibernate.Cfg.Environment.ConnectionStringName, "Sample");
19. 再次运行程序即可。
NHibernate为配置包含了一个新的fluent API。这个API定义在Loquacious命名空间中,它包含了配置的所有方面。
跟之前一样,我们不讨论太多的理论,而是直接深入例子。在这个例子中,使用前面提到的一些概念,如值对象的使用和NHibernate Profiler工具。
1. 打开Visual Studio,创建一个控制台应用程序:LoquaciousConfigurationSample。
2. 在这个例子中我们还是使用SQLite作为,因此需要相应的调整一下项目设置。在Solution Explorer,右击选择属性,在Application选项卡中选择.NET Framework 3.5作为Target framework,在Build选项卡中确保Platform target设置为Any CPU。nhibernate
4. 在解决方案中添加一个解决方案文件夹:Schema,在其中添加两个文件:nhibernate-configuration.xsd和nhibernate-mapping.xsd。
5. 在项目中添加对以下程序集的引用:

NHibernate.dll
NHibernate.ByteCode.Castle.dll
HibernatingRhinos.Profiler.Appender.dll
System.Data.SQLite.dll
6. 如果项目中没有app.config文件,则添加一个并添加如下代码:
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="Sample" connectionString="data source=loquaciousConfig.dbf;version=3;new=true;"/> </connectionStrings> </configuration>
7. 在项目中添加一个类文件:Person.cs,添加如下代码定义Person实体:
public class Person { public Guid Id { get; set; } public Name Name { get; set; } public string SSN { get; set; } public DateTime Birthdate { get; set; } }
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-39178-5.html
西方列强普遍认为吨位和火炮数量占优的日本海军胜算较大