Objective
This chapter is a complete NHibernate simple, the original text with fluent NHibernate do mapping, but I use NHibernate3.2 version, so 3.2 conformist instead of fluent NHibernate.
From here we will learn the general steps to use NHibernate:
1. Define Model
2. Map model
3. Defining the Configuration
4.1 Creating a database based on configuration
4.2 According to the configuration buildsessionfactory
5. Opensession with Sessionfactory Object
6. Use Session object to do database operation
1. Define Model
Defines two classes of product,category
public class category{public virtual int Id {get; set;} Public virtual string Name {get; set;} Public virtual string Description {get; set;}}
public class product{public virtual int Id {get; set;} Public virtual string Name {get; set;} Public virtual string Description {get; set;} Public virtual decimal UnitPrice {get; set;} public virtual int ReorderLevel {get; set;} Public virtual bool Discontinued {get; set;} Public virtual category category {get; set;}}
Class diagram: 2. Mapping model
Create two classes of Categorymap,productmap
public class productmap:classmapping<product>{public productmap () {this . Id (p = p.id, map = = map. Generator (generators.identity); }); This. Property (P = p.description); This. Property (P = p.discontinued); This. Property (P = p.name); This. Property (P = p.reorderlevel); This. Property (P = p.unitprice); This. Manytoone (p = p.category);} }
The primary key above is automatically incremented, and the category attribute of product is mapped with Manytoone
3. Defining the Configuration
Public Form1 () { InitializeComponent (); Configuration. Databaseintegration ( x = { x.dialect<sqlitedialect> (); X.driver<sqlite20driver> (); x.connectionstring = connstring; }); var mapper = new Modelmapper (); Mapper. Addmapping<productmap> (); Mapper. Addmapping<categorymap> (); var hbmmapping = Mapper.compilemappingforallexplicitlyaddedentities (); Configuration. Addmapping (hbmmapping); Debug.WriteLine (Hbmmapping.asstring ());}
Here are a few things to do, set the database dialect, database driver, database connection string, and add the mappings to the configuration.
XML mappings for Debug.WriteLine output:
View Code4.1 Creating a database based on configuration
The database can be built with configuration
private void Btcreatedatabase_click (object sender, EventArgs e) { var sc = new Schemaexport (configuration); Sc. Setoutputfile (@ "Db.sql"). Execute (False, False, false); Sc. Create (False, True);}
The resulting database diagram
4.2 According to the configuration buildsessionfactory
With the configuration, you can buildsessionfactory
Private Isessionfactory Createsessionfactory () { return configuration. Buildsessionfactory ();}
5. Open session with Sessionfactory object
With Isessionfactory, you can opensession.
private void Btncreatesession_click (object sender, EventArgs e) { var factory = Createsessionfactory (); using (var session = Factory. Opensession ()) { //do something with the session }}
6. Use Session object to do database operation
With ISession objects, like doing database operations, you can think of the ISession object as an object database
Add to
private void Btaddcategory_click (object sender, EventArgs e) { var factory = Createsessionfactory (); using (var session = Factory. Opensession ()) { var category = new category { Name = txtcategoryname.text, Description = Txtcategorydescription.text }; var id = session. Save (category); MessageBox.Show (ID. ToString ());} }
Inquire
private void Btloadall_click (object sender, EventArgs e) { var factory = Createsessionfactory (); using (var session = Factory. Opensession ()) { var categories = Session. Query<category> () . (c = c.id) . Select (c = c.name + "-" + c.description). ToArray (); Lbcategory.datasource = categories; }}
NHibernate 3 Beginner ' s Guide