前言

这一章是一个完整的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. [ python ] 正则表达式及re模块

    正则表达式 正则表达式描述: 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个‘规则字符串’,这个‘规则字符串’用来    表达对字符串的一种过滤 ...

  2. python 随机字符串

    pip3 install pillow 读取硬盘中的文件,在页面显示 f = open('static/imgs/yj.png','rb') data = f.read() f.close() ret ...

  3. IDE按住ctrl 打开单元 无效时 的方法

    一般打开单元无效时 是由于程序有错误,若程序没有错误 可以重新build一下 再试. 若实在不行 就右键---open at cursor

  4. ASPLOS'17论文导读——SC-DCNN: Highly-Scalable Deep Convolutional Neural Network using Stochastic Computing

    今年去参加了ASPLOS 2017大会,这个会议总体来说我感觉偏系统和偏软一点,涉及硬件的相对少一些,对我这个喜欢算法以及硬件架构的菜鸟来说并不算非常契合.中间记录了几篇相对比较有趣的paper,今天 ...

  5. 全栈Python 必备库

    强大的库: 转自:微信公众号 Python最棒的地方之一,就是大量的第三方库,覆盖之广,令人惊叹.Python 库有一个缺陷就是默认会进行全局安装.为了使每个项目都有一个独立的环境,需要使用工具vir ...

  6. 【PAT】1011. A+B和C (15)

    1011. A+B和C (15) 给定区间[-231, 231]内的3个整数A.B和C,请判断A+B是否大于C. 输入格式: 输入第1行给出正整数T(<=10),是测试用例的个数.随后给出T组测 ...

  7. 浅谈BUFF设计

    Buff在游戏中无处不在,比如WOW.DOTA.LOL等等,这些精心设计的BUFF,让我们击节赞叹,沉迷其中. 问:BUFF的本质是什么? BUFF 是对一项或多项数据进行瞬间或持续作用的集合.(持续 ...

  8. ServiceWorker pwa缓存

    index.js if ( navigator.serviceWorker ) { console.log("cache index") window.addEventListen ...

  9. LoadRunner中自定义C函数实现字符串替换

    .在globals.h 中定义一个函数ReplaceStr,实现字符串的替换: int ReplaceStr(char* sSrc, char* sMatchStr, char* sReplaceSt ...

  10. bzoj 1108

    思路:水题, 将所有点按x轴对称反转,就变成了两堆点的坐标和的差.. #include<bits/stdc++.h> #define LL long long #define fi fir ...