Fluent NHibernate and Mysql,SQLite,PostgreSQL
http://codeofrob.com/entries/sqlite-csharp-and-nhibernate.html
https://code.google.com/archive/p/csharp-sqlite/downloads
https://github.com/davybrion/NHibernateWorkshop
MySQL
sql:
#my sql test DROP TABLE Department; CREATE TABLE Department
(
Id INT AUTO_INCREMENT PRIMARY KEY,
DepName VARCHAR(50),
PhoneNumber VARCHAR(50)
); INSERT INTO Department(DepName,PhoneNumber) VALUES('IT','222323'); select * from Department; CREATE TABLE Employee
(
Id INT AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(50),
Position VARCHAR(50),
DepartmentId INT not null,
CONSTRAINT dememp_id foreign key(DepartmentId) REFERENCES Department(Id)
ON DELETE NO ACTION
ON UPDATE CASCADE
); INSERT INTO Employee(FirstName,Position,DepartmentId) VALUES('sd','SE',1)
/// <summary>
///MySQL 创建ISessionFactory
/// </summary>
/// <returns></returns>
public static ISessionFactory GetSessionFactory()
{
if (_sessionFactory == null)
{
lock (_objLock)
{
if (_sessionFactory == null)
{
//配置ISessionFactory
_sessionFactory = FluentNHibernate.Cfg.Fluently.Configure()
//数据库配置
.Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard
.ConnectionString(c=>c.Server("")
.Database("geovindu")
.Password("520")
.Username("root"))
)
.Mappings(m => m
//.FluentMappings.PersistenceModel
//.FluentMappings.AddFromAssembly();
.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())) //用法注意
.BuildSessionFactory(); // Fluently.Configure().Database(
// MySqlConfiguration.Standard.ConnectionString(
// c => c.FromConnectionStringWithKey("ConnectionString")
// )
//)
//.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyAutofacModule>())
//.BuildSessionFactory()) }
}
}
return _sessionFactory; }
/// <summary>
/// 重置Session
/// </summary>
/// <returns></returns>
public static ISession ResetSession()
{
if (_session.IsOpen)
_session.Close();
_session = _sessionFactory.OpenSession();
return _session;
}
/// <summary>
/// 打开ISession
/// </summary>
/// <returns></returns>
public static ISession GetSession()
{
GetSessionFactory();
if (_session == null)
{
lock (_objLock)
{
if (_session == null)
{
_session = _sessionFactory.OpenSession();
}
}
}
return _session;
}
SQLite sql:
--sqlite
--create database geovindu; --use geovindu; drop table Department; CREATE TABLE Department
(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
DepName VARCHAR(50),
PhoneNumber VARCHAR(50)
); INSERT INTO Department(DepName,PhoneNumber) VALUES('IT','222323'); select * from Department; drop table Employee; CREATE TABLE Employee
(
Id INTEGER PRIMARY KEY AUTOINCREMENT ,
FirstName VARCHAR(50),
Position VARCHAR(50),
DepartmentId INT not null,
CONSTRAINT dememp_id foreign key(DepartmentId) REFERENCES Department(Id)
ON DELETE NO ACTION
ON UPDATE CASCADE
); INSERT INTO Employee(FirstName,Position,DepartmentId) VALUES('sd','SE',1) select * from Employee
https://github.com/ladenedge/FluentNHibernate.Cfg.Db.CsharpSqlite
SQLite (测试ISessionFactory还存在问题)
/// <summary>
/// http://frankdecaire.blogspot.com/2014/04/fluent-nhibernate-unit-testing-using.html
/// </summary>
public static class SQLLiteSessionFactory
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration
.Standard
.InMemory()
.UsingFile("sibodu.db")
)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateTestProject.Entites.Department>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateTestProject.Entites.Employee>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
/// http://www.cnblogs.com/vingi/articles/4302497.html
/// http://codeofrob.com/entries/sqlite-csharp-and-nhibernate.html
/// http://frankdecaire.blogspot.com/2014/04/fluent-nhibernate-unit-testing-using.html
/// <summary>
/// Nhibernate
/// </summary>
public class MonoSQLiteDriver : NHibernate.Driver.ReflectionBasedDriver
{
public MonoSQLiteDriver()
: base(
"Mono.Data.Sqlite",
"Mono.Data.Sqlite",
"Mono.Data.Sqlite.SqliteConnection",
"Mono.Data.Sqlite.SqliteCommand")
{
} public override bool UseNamedPrefixInParameter
{
get
{
return true;
}
} public override bool UseNamedPrefixInSql
{
get
{
return true;
}
} public override string NamedPrefix
{
get
{
return "@";
}
} public override bool SupportsMultipleOpenReaders
{
get
{
return false;
}
}
} /// <summary>
/// Fluent NHibernate
///
/// </summary>
public class MonoSQLiteConfiguration : PersistenceConfiguration<MonoSQLiteConfiguration>
{
public static MonoSQLiteConfiguration Standard
{
get { return new MonoSQLiteConfiguration(); }
}
/// <summary>
///
/// </summary>
public MonoSQLiteConfiguration()
{
Driver<MonoSQLiteDriver>();
Dialect<SQLiteDialect>();
Raw("query.substitutions", "true=1;false=0");
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public MonoSQLiteConfiguration InMemory()
{
Raw("connection.release_mode", "on_close");
return ConnectionString(c => c
.Is("Data Source=:memory:;Version=3;"));//New=True; }
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public MonoSQLiteConfiguration UsingFile(string fileName)
{
return ConnectionString(c => c
.Is(string.Format("Data Source={0};Version=3;Pooling=true;FailIfMissing=false;UTF8Encoding=True;", fileName)));//New=True;
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="password"></param>
/// <returns></returns>
public MonoSQLiteConfiguration UsingFileWithPassword(string fileName, string password)
{
return ConnectionString(c => c
.Is(string.Format("Data Source={0};Version=3;New=True;Password={1};", fileName, password)));
}
}
PostgreSQL
//https://developer.jboss.org/wiki/DatabasessupportedbyNHibernate
//https://github.com/daanl/Fluent-NHibernate--PostgreSQL-column-array
sql:
--PostgreSQL
drop table Department; CREATE TABLE Department
(
Id SERIAL PRIMARY KEY,
DepName VARCHAR(50),
PhoneNumber VARCHAR(50)
); INSERT INTO Department(DepName,PhoneNumber) VALUES('IT','222323'); select * from Department; drop table Employee; CREATE TABLE Employee
(
Id SERIAL PRIMARY KEY ,
FirstName VARCHAR(50),
Position VARCHAR(50),
DepartmentId INT not null,
CONSTRAINT dememp_id foreign key(DepartmentId) REFERENCES Department(Id)
); INSERT INTO Employee(FirstName,Position,DepartmentId) VALUES('sd','SE',1) select * from Employee
/// <summary>
/// PostgreSQL
/// </summary>
/// <returns></returns>
public static ISessionFactory GetSessionFactory()
{ if (_sessionFactory == null)
{
lock (_objLock)
{
if (_sessionFactory == null)
{
var connectionStr = "Server=localhost;Port=5432;Database=geovindu;User Id=postgres;Password=888;";
ISessionFactory sessionFactory = Fluently
.Configure()
.Database(PostgreSQLConfiguration.Standard.ConnectionString(connectionStr).ShowSql())
.Mappings(m => m.FluentMappings
//AddFromAssemblyOf<FluentNHibernateHelper>()) //TypeOfFluentNHibernateMapping
.AddFromAssembly(Assembly.GetExecutingAssembly())) //用法注意
//.ExposeConfiguration(CreateSchema)
.BuildSessionFactory(); }
}
}
return _sessionFactory; }
Fluent NHibernate and Mysql,SQLite,PostgreSQL的更多相关文章
- 关于PDF.NET开发框架对Mysql Sqlite PostgreSQL数据库分页支持的个人看法
关于PDF.NET开发框架的名字由来 在设计www.pwmis.com站点的时候,考虑到架构的兼容性和将来升级的可能性,最重要的是没有足够的时间去为网站添加和维护很多复杂的程序,所以在借鉴前人成功经 ...
- SQLite vs MySQL vs PostgreSQL:关系型数据库比较
自1970年埃德加·科德提出关系模型之后,关系型数据库便开始出现,经过了40多年的演化,如今的关系型数据库种类繁多,功能强大,使用广泛.面对如此之多的关系型数据库,我们应该如何权衡找出适合自己应用场景 ...
- 【翻译】Fluent NHibernate介绍和入门指南
英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...
- Fluent Nhibernate之旅(五)--利用AutoMapping进行简单开发
Fluent Nhibernate(以下简称FN)发展到如今,已经相当成熟了,在Nhibernate的书中也相应的推荐了使用FN来进行映射配置,之前写的FN之旅至今还有很多人会来私信我问题,说来惭愧, ...
- 对Oracle 、SQL Server、MySQL、PostgreSQL数据库优缺点分析
对Oracle .SQL Server.MySQL.PostgreSQL数据库优缺点分析 Oracle Database Oracle Database,又名Oracle RDBMS,或简称Oracl ...
- Redis/Mysql/SQLite/MongoDB 数据库对比
一.Redis: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...
- 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)
在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...
- [Fluent NHibernate]第一个程序
目录 写在前面 Fluent Nhibernate简介 基本配置 总结 写在前面 在耗时两月,NHibernate系列出炉这篇文章中,很多园友说了Fluent Nhibernate的东东,也激起我的兴 ...
- [Fluent NHibernate]一对多关系处理
目录 写在前面 系列文章 一对多关系 总结 写在前面 上篇文章简单介绍了,Fluent Nhibernate使用代码的方式生成Nhibernate的配置文件,以及如何生成持久化类的映射文件.通过上篇的 ...
随机推荐
- MyEclipse中拷贝J2EE项目,发布到tomcat中名字一样的解决办法
修改Eclipse工作空间下新拷贝项目下.settings文件夹中org.eclipse.wst.common.component的两个属性值. 为新项目名字: <?xml version=&q ...
- 3.C#中的多重委托
阅读目录 一:多重委托概述 二:多重委托实例 一:多重委托概述 1.委托的调用其实是一个调用列表,可以同时调用多个不同的方法 2.第1个委托加上第2个委托赋予第3个委托,相当于把两个方法按顺 ...
- Windows调试学习笔记:(二)WinDBG调试.NET程序示例
好不容易把环境打好了,一定要试试牛刀.我创建了一个极其简单的程序(如下).让我们期待会有好的结果吧,阿门! using System; using System.Collections.Generic ...
- 《Microsoft SQL Server 2008 Internals》读书笔记
http://www.cnblogs.com/downmoon/archive/2010/01/26/1656411.html
- swift 附属脚本
附属脚本是访问对象,集合或序列的快捷方式 struct STest{ let constValue:Int subscript(count:Int)->Int{ return count*con ...
- EPLAN P8导线颜色的设置
P8里的导线应称为"连接",连接的颜色代表了其电位的传递路径,如可以给三相电源设置成黑色,PE设为绿色,N设为蓝色等等. P8中电位在连接和元器件中传递的方法是由元器件的连接点属 ...
- git pull 指定版本
git init git remote add origin git@bitbucket.org:huashiyiqike/lstm-hf.git git pull origin master
- TL(简单)
TL time limit per test 2 seconds memory limit per test 256 megabytes input standard input output sta ...
- 安装虚拟机VMware tools
不懂得安装虚拟机VMware tools的想必都是刚在虚拟机上玩系统初学者,无疑我们对虚拟机的了解并不深,这使得本来很容易安装的VMware tools在我们安装时变得复杂而又难以琢磨,到头一直的付出 ...
- Tmux 常用命令与快捷键
命令与别名 attach-session [-dr] [-t target-session] 别名 attach,连接会话. detach-client [-P] [-a] [-s target-se ...