NHibernate 3 Beginner's Guide
前言
这一章是一个完整的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的更多相关文章
- A Beginner's Guide to Paxos
Google Drive: A Beginner's Guide to Paxos The code ideas of Paxos protocol: 1) Optimistic concurrenc ...
- Beginner's Guide to Python-新手指导
Refer English Version: http://wiki.python.org/moin/BeginnersGuide New to programming? Python is free ...
- A Beginner's Guide To Understanding Convolutional Neural Networks(转)
A Beginner's Guide To Understanding Convolutional Neural Networks Introduction Convolutional neural ...
- (转)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 ...
- (转)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 ...
- 新手教程之:循环网络和LSTM指南 (A Beginner’s Guide to Recurrent Networks and LSTMs)
新手教程之:循环网络和LSTM指南 (A Beginner’s Guide to Recurrent Networks and LSTMs) 本文翻译自:http://deeplearning4j.o ...
- 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 ...
- A Beginner’s Guide to Eigenvectors, PCA, Covariance and Entropy
A Beginner’s Guide to Eigenvectors, PCA, Covariance and Entropy Content: Linear Transformations Prin ...
- 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 ...
随机推荐
- <转>MYSQL数据库数据拆分之分库分表总结
数据存储演进思路一:单库单表 单库单表是最常见的数据库设计,例如,有一张用户(user)表放在数据库db中,所有的用户都可以在db库中的user表中查到. 数据存储演进思路二:单库多表 随着用户数量的 ...
- Selenium_Grid
Selenium Grid 工作原理 Grid是一种分布式测试工具,整个结果由一个hub主节点和若干个node代理节点组成. hub用来管理各个代理节点的注册和状态信息,并且接收远程客户端代码请求调用 ...
- DNS使用TCP还是UDP?
DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP栈也算是个另类.下面将介绍DNS分别在什么情况下使用这两种协议. TCP与UDP简介 TCP ...
- 百度NLP面试题
C++ : 1.拷贝构造函数和重载=符分别在什么情况下被调用,实现有什么区别 2.虚函数的目的,虚函数和模板类的区别,如何找到虚函数 常规算法: 1. 如何输出一个集合的所有真子集,递归和非递 ...
- 使用OpenSSL自签发服务器https证书
OpenSSL官方推荐win32可执行文件版下载:http://www.slproweb.com/products/Win32OpenSSL.html ca.key CA私钥: openssl gen ...
- char *s 和char s[]的区别
char *s 和 char s[] 的区别小结 博客分类: C语言 c教育 . 最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区( ...
- ul>li中自定义属性后取值的问题
动态赋值的li: $.ajax({ type: "POST", url: "${base}/before/subDemand/listType", succes ...
- Typora ---一款简洁的Markdown编辑器
Typora BB in front 如果你是一个佛(lan)系(duo),内心文艺的程序员,并且你对其他Markdown编辑器的使用效果感觉不是很好的话,可以来了解一下该软件Typora. What ...
- 邝斌带你飞之数论专题--Maximum GCD UVA - 11827
Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible p ...
- vmware12安装centos7系统详解
1.首先需要准备的工具有vmware12和contos7的系统. vmvare12下载地址: http://pan.baidu.com/s/1i5vH50D contos7我自己使用的为1511版本. ...