單表的建立
看了好久NHibernate的資料,因為自己e文太菜,所以只有看看我們前人的資料,今天自己去NHibernate網站下了新的dll,然后按照以前搜集的文檔,作一個單表的結構,發現有幾個地方和以前的不一樣.
首先我拷貝了一個hibernate.cfg.xml文件:
順便說一下,CodeSmith40使用測試序列號,然后用以前的3x的注冊機就可以注冊了.
修改User.cs裡面加上virtual
才可以正常編譯。
這個博客保存好像比較慢。。。。。。就長話短說了
首先我拷貝了一個hibernate.cfg.xml文件:
1<?xml version="1.0" encoding="utf-8" ?>
2<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
3 <session-factory name="temp0704">
4 <!-- 属性 -->
5 <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
6 <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
7 <property name="connection.connection_string">server=.;database=cy_dwh;uid=sa;pwd=sa;</property>
8 <property name="show_sql">false</property>
9 <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
10 <property name="use_outer_join">true</property>
11 <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
12 <!-- 映射文件 -->
13 <mapping assembly="temp0704" />
14 </session-factory>
15</hibernate-configuration>
16
17
18
19
20
然後是一個簡單的表2<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
3 <session-factory name="temp0704">
4 <!-- 属性 -->
5 <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
6 <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
7 <property name="connection.connection_string">server=.;database=cy_dwh;uid=sa;pwd=sa;</property>
8 <property name="show_sql">false</property>
9 <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
10 <property name="use_outer_join">true</property>
11 <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
12 <!-- 映射文件 -->
13 <mapping assembly="temp0704" />
14 </session-factory>
15</hibernate-configuration>
16
17
18
19
20
1if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[User]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
2drop table [dbo].[User]
3GO
4
5CREATE TABLE [dbo].[User] (
6 [LogonID] [varchar] (20) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
7 [Name] [varchar] (40) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
8 [Password] [varchar] (20) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
9 [EmailAddress] [varchar] (40) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
10 [LastLogon] [datetime] NULL
11) ON [PRIMARY]
12GO
13
14ALTER TABLE [dbo].[User] ADD
15 PRIMARY KEY CLUSTERED
16 (
17 [LogonID]
18 ) ON [PRIMARY]
19GO
20
21
我用CodeSmith40產生了它的數據類User.cs和映射文件User.hbm.xml2drop table [dbo].[User]
3GO
4
5CREATE TABLE [dbo].[User] (
6 [LogonID] [varchar] (20) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
7 [Name] [varchar] (40) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
8 [Password] [varchar] (20) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
9 [EmailAddress] [varchar] (40) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
10 [LastLogon] [datetime] NULL
11) ON [PRIMARY]
12GO
13
14ALTER TABLE [dbo].[User] ADD
15 PRIMARY KEY CLUSTERED
16 (
17 [LogonID]
18 ) ON [PRIMARY]
19GO
20
21
順便說一下,CodeSmith40使用測試序列號,然后用以前的3x的注冊機就可以注冊了.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="temp0704.User, temp0704" table="`User`">
<id name="Id" type="String" unsaved-value="null">
<column name="LogonID" length="20" sql-type="varchar" not-null="true" unique="true" index="PK__Users__7B5B524B"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="Name" length="40" sql-type="varchar" not-null="false"/>
</property>
<property name="Password" type="String">
<column name="Password" length="20" sql-type="varchar" not-null="false"/>
</property>
<property name="EmailAddress" type="String">
<column name="EmailAddress" length="40" sql-type="varchar" not-null="false"/>
</property>
<property name="LastLogon" type="DateTime">
<column name="LastLogon" length="8" sql-type="datetime" not-null="false"/>
</property>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="temp0704.User, temp0704" table="`User`">
<id name="Id" type="String" unsaved-value="null">
<column name="LogonID" length="20" sql-type="varchar" not-null="true" unique="true" index="PK__Users__7B5B524B"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="Name" length="40" sql-type="varchar" not-null="false"/>
</property>
<property name="Password" type="String">
<column name="Password" length="20" sql-type="varchar" not-null="false"/>
</property>
<property name="EmailAddress" type="String">
<column name="EmailAddress" length="40" sql-type="varchar" not-null="false"/>
</property>
<property name="LastLogon" type="DateTime">
<column name="LastLogon" length="8" sql-type="datetime" not-null="false"/>
</property>
</class>
</hibernate-mapping>
1using System;
2using System.Collections;
3
4namespace temp0704
5{
6 User
94}
修改兩個xml裡面urn:nhibernate-configuration-2.0為urn:nhibernate-configuration-2.22using System.Collections;
3
4namespace temp0704
5{
6 User
94}
修改User.cs裡面加上virtual
才可以正常編譯。
這個博客保存好像比較慢。。。。。。就長話短說了