NHibernate is a small item (1) Unhealthy willow

xiaoxiao2021-03-06  41

Only in practical use can really encounter problems, and continuously improve the problem in the process of solving the problem online reference http://blog.aspcool.com/tim/posts/1133.aspx http: //nhibernate.3yee. Com / Archive / 2004/04/26 / 439.aSPX, so you intend to write a small message based on NHibernate, first build a database first

Create

TABLE

[

DBO

]

.

[

G_guestbook

]

(

[

id

]

[

int

]

Identity

(

1

,

1

)

NOT

NULL

,

[

UserID

]

[

int

]

NOT

NULL

,

[

Username

]

[

nvarchar

]

(

20

COLLATE_PRC_CI_AS

NOT

NULL

,

[

Title

]

[

nvarchar

]

(

100

COLLATE_PRC_CI_AS

NOT

NULL

,

[

article

]

[

NText

]

Collate Chinese_PRC_CI_AS

NULL

,

[

Pubtime

]

[

Datetime

]

NOT

NULL

)

On

[

Primary

]

TEXTIMAGE_ON

[

Primary

]

Go

Create

TABLE

[

DBO

]

.

[

G_users

]

(

[

id

]

[

int

]

Identity

(

1

,

1

)

NOT

NULL

,

[

Name

]

[

nvarchar

]

(

20

COLLATE_PRC_CI_AS

NOT

NULL

,

[

Email

]

[

nvarchar

]

(

40

COLLATE_PRC_CI_AS

NULL

,

[

Password

]

[

nvarchar

]

(

20

COLLATE_PRC_CI_AS

NOT

NULL

,

[

Flag

]

[

int

]

NOT

NULL

,

[

Regtime

]

[

Datetime

]

NULL

)

Very simple structure, you can use it,

The second step is to open VS.NET to create a new one

Guestbook.data's project, I have to thank the flying eagle, providing COOL CODER good stuff, although nothing perfect, but the generated entity class, each mapping file can add to me The project has saved a lot of time.

My two entities and mapping files

Using

System;

Namespace

Guestbook.data

{Public class guestbooks {public guestbooks () {} private System.String _article; public System.String article {get {return _article;} set {_article = value;}} private System.Int32 _id; public System.Int32 id {get {return _id;} set {_id = value;}} private System.DateTime _pubtime; public System.DateTime pubtime {get {return _pubtime;} set {_pubtime = value;}} private System.String _title; public System.String title {get {return _title;} set {_title = value;}} private System.Int32 _userid; public System.Int32 userid {get {return _userid;} set {_userid = value;}} private System.String _username; public System. String username {get {return_usern AME;} set {_Username = value;}}}} using

System;

Namespace

Guestbook.data

{Public class users {public users () {} private System.Int32 _flag; public System.Int32 flag {get {return _flag;} set {_flag = value;}} private System.String _email; public System.String email {get {return _email;} set {_email = value;}} private System.Int32 _id; public System.Int32 id {get {return _id;} set {_id = value;}} private System.DateTime _regtime; public System.DateTime regtime {get {return _regtime;} set {_regtime = value;}} private System.String _Name; public System.String Name {get {return _Name;} set {_Name = value;}} private System.String _password; public System. String password {get {return _password;} set {_passw ORD = Value;}}}} There are two mapping files

XML Version = "1.0" encoding = "UTF-8"

?>

<

Hibernate-maping

XMLNS

= "URN: NHIBERNATE-MAPPING-2.0"

>

<

Class

Name

= "Guestbook.data.users, guestbook.data"

TABLE

= "G_users"

>

<

id

Name

= "id"

Column

= "id"

Type

= "INT32"

>

<

Generator

Class

= "Identity"

/>

id

>

<

Property

Name

= "Name"

Type

= "String (20)"

Column

= "Name"

/>

<

Property

Name

= "email"

Type

= "String (40)"

Column

= "email"

/>

<

Property

Name

= "Password" TYPE

= "String (20)"

Column

= "Password"

/>

<

Property

Name

= "flag"

Type

= "INT32"

Column

= "flag"

/>

<

Property

Name

= "Regtime"

Type

= "DateTime"

Column

= "Regtime"

/>

Class

>

Hibernate-maping

>

XML Version = "1.0" encoding = "UTF-8"

?>

<

Hibernate-maping

XMLNS

= "URN: NHIBERNATE-MAPPING-2.0"

>

<

Class

Name

= "Guestbook.Data.Guestbooks, Guestbook.Data"

TABLE

= "G_guestbook"

>

<

id

Name

= "id"

Column

= "id"

Type

= "INT32"

>

<

Generator

Class

= "Identity"

/>

id

>

<

Property

Name

= "UserID"

Type

= "INT32"

Column

= "UserID"

/>

<

Property

Name

= "Username"

Type

= "String (20)"

Column

= "Username"

/>

<

Property

Name

= "Title"

Type

= "String (100)"

Column

= "Title"

/>

<

Property

Name

= "article"

Type

= "String"

Column

= "article"

/>

<

Property

Name

= "Pubtime"

Type

= "DateTime"

Column

= "Pubtime"

/>

Class

>

Hibernate-maping

>

Since I use an automatic number, I have to

change to

Add a new project named guestbook.test write a test file

The main purpose here is to verify the correctness of the configuration, so some test files are also written.

Using

System;

Using

System.collections;

Using

NhiBernate;

Using

NHibernate.cfg;

Using

NUNIT.FRAMEWORK;

Using

Guestbook.data;

Namespace

Guestbook.test

{/ ** ////

/// DatalayerUserstest's summary description. /// [TestFixture] public class DataLayerUsersTest {public DataLayerUsersTest () {} [Test] public void testAdd () {Configuration cfg = new Configuration (); cfg.AddXmlFile ( "users.hbm.xml"); ISessionFactory f = cfg.BuildSessionFactory (); ISession s = f.OpenSession (); ITransaction t = s.BeginTransaction (); users newUser = new users (); newUser.Name = "papersnake"; newUser.password = "24976904" Newuser.email = "papersnakes@gmail.com"; newuser.regtime = datetime.now; s.save (newuser); t.commit (); s.close ();}}}

Another file is also small, it's not going to come out, compile

Open NUNIT, RUN, show a beautiful green.

The mood is not a class, Wowka

转载请注明原文地址:https://www.9cbs.com/read-68754.html

New Post(0)