前言

这一章是一个完整的NHibernate的Simple,原文中用Fluent NHibernate做映射,但我使用NHibernate3.2版本,所以3.2的Conformist代替Fluent NHibernate.

从这里我们将学习到使用NHibernate的一般步骤:

1.定义Model

2.映射Model

3.定义配置

4.1根据配置创建数据库

4.2根据配置BuildSessionFactory

5.用SessionFactory对象OpenSession

6.用session对象做数据库操作

1.定义Model

定义两个类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; }
}
类关系图:

2.映射Model

创建两个类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);
}
}

上面主键自动递增,Product的Category属性用ManyToOne映射

3.定义配置

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());
}

这里做了几件事,设置数据库方言,数据库驱动,数据库连接字符串,把映射添加到配置中.

Debug.WriteLine输出的xml映射:


4.1根据配置创建数据库

有了配置就可以生成数据库

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);
}

生成的数据库关系图

4.2根据配置BuildSessionFactory

有了配置就可以BuildSessionFactory

private ISessionFactory CreateSessionFactory()
{
return configuration.BuildSessionFactory();
}

5.用SessionFactory对象打开Session

有了ISessionFactory 就可以OpenSession

private void btnCreateSession_Click(object sender, EventArgs e)
{
var factory = CreateSessionFactory();
using (var session = factory.OpenSession())
{
// do something with the session
}
}

6.用session对象做数据库操作

有了ISession对象,就像做数据库操作,可以把ISession对象想象成对象化的数据库

添加

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());
}
}

查询

private void btLoadAll_Click(object sender, EventArgs e)
{
var factory = CreateSessionFactory();
using (var session = factory.OpenSession())
{
var categories = session.Query<Category>()
.OrderBy(c => c.Id)
.Select(c => c.Name + "-" + c.Description).ToArray();
lbCategory.DataSource = categories;
}
}

NHibernate 3 Beginner's Guide的更多相关文章

  1. A Beginner's Guide to Paxos

    Google Drive: A Beginner's Guide to Paxos The code ideas of Paxos protocol: 1) Optimistic concurrenc ...

  2. Beginner's Guide to Python-新手指导

    Refer English Version: http://wiki.python.org/moin/BeginnersGuide New to programming? Python is free ...

  3. A Beginner's Guide To Understanding Convolutional Neural Networks(转)

    A Beginner's Guide To Understanding Convolutional Neural Networks Introduction Convolutional neural ...

  4. (转)A Beginner's Guide To Understanding Convolutional Neural Networks Part 2

    Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolution ...

  5. (转)A Beginner's Guide To Understanding Convolutional Neural Networks

    Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolution ...

  6. 新手教程之:循环网络和LSTM指南 (A Beginner’s Guide to Recurrent Networks and LSTMs)

    新手教程之:循环网络和LSTM指南 (A Beginner’s Guide to Recurrent Networks and LSTMs) 本文翻译自:http://deeplearning4j.o ...

  7. Photography theory: a beginner's guide(telegraph.co.uk)

    By Diane Smyth, Tim Clark, Rachel Segal Hamilton and Lewis Bush 11:00AM BST 09 Jun 2014   Have you r ...

  8. A Beginner’s Guide to Eigenvectors, PCA, Covariance and Entropy

    A Beginner’s Guide to Eigenvectors, PCA, Covariance and Entropy Content: Linear Transformations Prin ...

  9. A Beginner’s Guide to the OUTPUT Clause in SQL Server

    原文 A Beginner’s Guide to the OUTPUT Clause in SQL Server T-SQL supports the OUTPUT clause after the ...

随机推荐

  1. 20180830 安装git时报错,

    安装:https://blog.csdn.net/u013256816/article/details/54743470 解决问题:https://blog.csdn.net/daojibruce/a ...

  2. IP地址及子网--四种IP广播地址

    国际规定:把所有的IP地址划分为 A,B,C,D,E. 类默认子网掩码:A类为 255.0.0.0; B类为 255.255.0.0; C类为 255.255.255.0.子网掩码是一个32位地址,用 ...

  3. nginx配置tomcat集群

    显示nginx的核心配置 #user nobody;worker_processes 1; events { worker_connections 1024;    #并发连接数} http { in ...

  4. PhpStorm设置函数注释模板

    *设置位置:"Settings"->"file templates"; 如下图,设置头部注释.类注释以及函数注释,时间.用户名.文件名称等随机改变的属性, ...

  5. C++函数的重载、覆盖和隐藏区别

    a.成员函数被重载的特征:(1)相同的范围(在同一个类中)(2)函数名字相同(3)参数不同(4)virtual 关键字可有可无b.覆盖是指派生类函数覆盖基类函数,特征是:(1)不同的范围(分别位于派生 ...

  6. 【Java】Java中常用的String方法

    本文转载于:java中常用的String方法 1 length()字符串的长度 String a = "Hello Word!"; System.out.println(a.len ...

  7. 修改MySQL事件

    MySQL允许您更改现有事件的各种属性. 要更改现有事件,请使用ALTER EVENT语句,如下所示: ALTER EVENT event_name ON SCHEDULE schedule ON C ...

  8. 洛谷P4139 上帝与集合的正确用法 [扩展欧拉定理]

    题目传送门 上帝与集合的正确用法 题目描述 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”. ...

  9. JAVA解析xml的四种方式比较

    1)DOM解析 DOM是html和xml的应用程序接口(API),以层次结构(类似于树型)来组织节点和信息片段,映射XML文档的结构,允许获取 和操作文档的任意部分,是W3C的官方标准 [优点] ①允 ...

  10. 【BZOJ 4171】 4171: Rhl的游戏 (高斯消元)

    4171: Rhl的游戏 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 74  Solved: 33[Submit][Status][Discuss] ...