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 ...
随机推荐
- apache 软件历史版本查询
http://archive.apache.org/dist/ http://archive.apache.org/dist/struts/binaries/
- IntelliJ IDEA 创建maven项目一次后,然后删除,再次保存到此目录下,提供此目录已经被占用的问题。
-------------------2017-02-14补充: 你看既然是创建过一次 不允许再次创建了,那么请问 第一次创建的 跑哪里去了,不仅仅是保存到了你指定的目录里,其实也默认安装到了 mav ...
- PHP任意文件上传漏洞CVE-2015-2348浅析
昨晚安全新闻爆出一个“PHP任意文件上传漏洞”,CVE编号为:CVE-2015-2348. 当时楼主正准备收拾东西回家,看到这个新闻心里一惊:失传江湖多年的0字符截断上传漏洞又重现了?而且还影响这么多 ...
- 关于语义化版本(semantic versioning or SemVer)
1 为什么要有SemVer? SemVer用来规范组件之间的依赖版本,它使用一个版本号来传递出组件的API的变化情况. 在理解这规范之后,看一眼依赖包的版本号,就知道API的变化(兼容性)程度,方便 ...
- [Matlab]Upper Triangularization & Back Substitution代码
用来做数值计算作业的代码,来自Numerical Methods Using Matlab (4th Edition) [John H. Mathews, Kurtis K. Fink],改了一下注释 ...
- python 函数操作
四.函数 定义: #!/usr/local/env python3 ''' Author:@南非波波 Blog:http://www.cnblogs.com/songqingbo/ E-mail:qi ...
- 转:vs无法调试解决方案
转:http://blog.csdn.net/jean7155/article/details/48223739 vs中无法加入断点进行调试的解决方案 [ 1] 以前也遇到过同样的问题,但没有问个为什 ...
- (15) go 方法
- [hdu3685]Rotational Painting 凸包 重心
大致题意: 给出一个多边形,问你有多少种放法可以使得多边形稳定得立在平面上. 先对多边形求重心,在求凸包,枚举凸包的边,如果重心没有在边的范围内,则不行 判断是否在范围内可用点积来判断 #includ ...
- CPPUNIT_TEST
(1) CPPUNIT_ASSERT(condition):判断condition的值是否为真,如果为假则生成错误信息. (2)CPPUNIT_ASSERT_MESSAGE(message, cond ...