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 ...
随机推荐
- strcpy、memcpy和memset之间的区别
今天刷题时遇到了这个问题,记录一下. strcpy比较简单,就是拷贝字符串,遇到'\0'时结束拷贝. memcpy用来做内存拷贝,可以拷贝任何数据类型的对象并指定拷贝数据的长度:char a[100] ...
- Python解释器【转载】
原文链接 0x01 简介 当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 0x02 Python ...
- Hadoop案例(五)过滤日志及自定义日志输出路径(自定义OutputFormat)
过滤日志及自定义日志输出路径(自定义OutputFormat) 1.需求分析 过滤输入的log日志中是否包含xyg (1)包含xyg的网站输出到e:/xyg.log (2)不包含xyg的网站输出到e: ...
- 【51nod】1239 欧拉函数之和
题解 写完上一道就开始写这个,大体上就是代码改了改而已= = 好吧,再推一下式子! \(\sum_{i = 1}^{n}i = \sum_{i = 1}^{n}\sum_{d | i}\phi(d) ...
- 【LOJ】#2350. 「JOI 2017/2018 决赛」月票购买
题解 首先求一个最短路图出来,最短路图就是这条边在最短路上就保留,否则就不保留,注意最短路图是一个有向图,一条边被保留的条件是 dis(S,u) + val(u,v) = dis(v,T)我们需要求两 ...
- 牛客练习赛19 D-托米去购物
最裸的最大流,没啥好说的.. #include<bits/stdc++.h> #define LL long long #define fi first #define se second ...
- (转)一些牛人榜样,多看看他们写的东西(后续整理牛人的blog等)
http://blog.csdn.net/ajian005/article/details/7697761
- 深度学习基础系列(三)| sigmoid、tanh和relu激活函数的直观解释
常见的激活函数有sigmoid.tanh和relu三种非线性函数,其数学表达式分别为: sigmoid: y = 1/(1 + e-x) tanh: y = (ex - e-x)/(ex + e-x) ...
- shell sh bash 概念
在shell脚本的开头往往有一句话来定义使用哪种sh解释器来解释脚本.目前研发送测的shell脚本中主要有以下两种方式:(1) #!/bin/sh(2) #!/bin/bash以上两种方式有什么区别? ...
- 转载:tar命令批量解压方法总结
由于linux的tar命令不支持批量解压,所以很多网友编写了好多支持批量解压的shell命令,收集了一下,供大家分享: 第一:for tar in *.tar.gz; do tar xvf $tar ...