<?xml version="1.0" encoding="utf-8" ?><configuration><!-- Add this element --><configSections><sectionname="hibernate-configuration"type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/></configSections><!-- Add this element --><hibernate-configurationxmlns="urn:nhibernate-configuration-2.2"><session-factory><!-- 数据库驱动 --><propertyname="dialect">NHibernate.Dialect.MsSql2012Dialect</property><!-- 数据库链接字符串 --><propertyname="connection.connection_string">Server=localhost\SQLEXPRESS;initial catalog=quickstart;Integrated Security=True
</property><!-- 映射配置 --><mappingassembly="QuickStart"/></session-factory></hibernate-configuration><!-- Leave the other sections unchanged --><system.web>
...
</system.web></configuration>
数据库表结构
Column|Type| Modifiers
--------+--------------+----------------------
CatId |char(32)|notnull,primarykey
Name | nvarchar(16)|notnull
Sex |nchar(1)|
Weight |real|
创建一个Cat映射类
publicclassCat{publicvirtualstring Id {get;set;}publicvirtualstring Name {get;set;}publicvirtualchar Sex {get;set;}publicvirtualfloat Weight {get;set;}}
添加一个映射配置文件Cat.hbm.xml,并将文件属性【内容】改为嵌入的资源
<?xml version="1.0" encoding="utf-8" ?><hibernate-mappingxmlns="urn:nhibernate-mapping-2.2"namespace="QuickStart"assembly="QuickStart"><classname="Cat"table="Cat"><!-- A 32 hex character is our surrogate key. It's automatically generated by NHibernate with the UUID pattern. --><idname="Id"><columnname="CatId"sql-type="char(32)"not-null="true"/><generatorclass="uuid.hex"/></id><!-- A cat has to have a name, but it shouldn't be too long. --><propertyname="Name"><columnname="Name"length="16"not-null="true"/></property><propertyname="Sex"/><propertyname="Weight"/></class></hibernate-mapping>