Initial Nhibernate experience

Source: Internet
Author: User

I recently studied nhib.pdf and found this article.Article, A Quickstart guide to nhib.pdf
In the original article, I thought it would be very simple, so I followed it step by step, but the results could not come out!

So I put the nunit and log4netSource codeAll of them are down. Join the solution and try to debug them one by one to see where the error is! The nightmare begins ......

Although these three open source projects have been heard (nunit is in use), they have not had time to study them carefully.Code. It seems like you have entered the canonoss maze with such a rash. How can this problem be solved...

Forget it. Start again!

0. Create the database nhib.pdf and the corresponding table as follows:
Create Database nhib.pdf
Go

Use nhib.pdf
Create Table users (
Logonid varchar (20) not null default '0 ',
Name varchar (40) default null,
Password varchar (20) default null,
Emailaddress varchar (40) default null,
Lastlogon datetime default null,
Primary Key (logonid)
)
Go

 

1. Use vs2003 to create a blank solution named nhibernatedemo. Add a classlibrary with the default name classlibrary1. Add the user. CS file as follows:
Using system;
Namespace classlibrary1
{
Public class user
{
Private string ID;
Private string username;
Private string password;
Private string emailaddress;
Private datetime lastlogon;

Public user ()
{
}

Public String ID
{
Get {return ID ;}
Set {id = value ;}
}

Public String Username
{
Get {return username ;}
Set {username = value ;}
}

Public String Password
{
Get {return password ;}
Set {Password = value ;}
}

Public String emailaddress
{
Get {return emailaddress ;}
Set {emailaddress = value ;}
}

Public datetime lastlogon
{
Get {return lastlogon ;}
Set {lastlogon = value ;}
}

}
}

2. Add the ing file user. HBM. xml. The content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.0">
<Class name = "classlibrary1.user, classlibrary1" table = "users">
<ID name = "ID" column = "logonid" type = "string (20)">
<Generator class = "assigned"/>
</ID>
<Property name = "username" column = "name" type = "string (40)"/>
<Property name = "password" type = "string (20)"/>
<Property name = "emailaddress" type = "string (40)"/>
<Property name = "lastlogon" type = "datetime"/>
</Class>
</Hibernate-mapping>

In the solution view, right-click the file and set its "generate operation" to "embedded resource", so that it will be packaged and released along with the class library.

3. For the nhibernatedemo solution, add a consoleapplication named consoleapplication1 by default. Add the following content to the main function in class1:
Configuration CFG = new configuration ();
Cfg. addassembly ("classlibrary1"); // classlibary1 is the name of the result file generated by the class library project.
 
Isessionfactory factory = cfg. buildsessionfactory ();
Isession session = factory. opensession ();
Itransaction transaction = session. begintransaction ();
 
User newuser = new user ();
Newuser. ID = "joe_cool ";
Newuser. Username = "Joseph cool ";
Newuser. Password = "ABC123 ";
Newuser. emailaddress = "joe@cool.com ";
Newuser. lastlogon = datetime. now;

// Tell nhibtasks that this object shocould be saved
Session. Save (newuser );

// Commit all of the changes to the DB and close the isession
Transaction. Commit ();
Session. Close ();

Add several namespaces at the beginning of the file:
Using nhib.pdf; // for other nhib.pdf API
Using Nhibernate. cfg; // For class configuration
Using classlibrary1; // For Class user

Of course, you must first Add the following reference for leleapplication1: classlibrary1.dll, Nhibernate. dll, so that compilation can pass.

 

4. Add the app. config file for consoleapplication1. The content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>

<Section name = "nhib.pdf"
Type = "system. configuration. namevaluesectionhandler, system,
Version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089"
/>
</Configsections>

<Nhib.pdf>
<Add
Key = "hibernate. Connection. provider"
Value = "nhib.pdf. Connection. driverconnectionprovider"
/>
<Add
Key = "hibernate. dialect"

Value = "nhibect. dialect. mssql2000dialect"
/>
<Add
Key = "hibernate. Connection. driver_class"
Value = "nhib.pdf. Driver. sqlclientdriver"
/>
<Add
Key = "hibernate. Connection. connection_string"
Value = "Server = localhost; initial catalog = nhibalog; user id = sa; Password = 123abc; min pool size = 2"
/>
</Nhib.pdf>
</Configuration>

If vs2003 is used, the IDE will automatically name the file "project name + exe. config" after compilation and copy it to the output directory. If not, copy app. config to the output directory and name it as above.

 

6. compile. Run. View the database results.

trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 68241

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.