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的配置文件,以及如何生成持久化类的映射文件.通过上篇的 ...
随机推荐
- Win7 64位 Visio反向工程(MySQL)
1 看Office的版本,我安装的是32位的版本,故要去MySQL的官网下载对应32位的ODBC驱动: http://dev.mysql.com/downloads/connector/odbc/ 2 ...
- solr searcher
solr searcher 前面我配置好了solr,并且数据库建立索引也完成了. 为php添加搜索 首先下载solrphp http://wiki.apache.org/solr/SolPHP 在so ...
- 开发Android必知的工具
程序开发有时候非常依赖使用的开发工具,好的完备的开发工具可以让开发人员的工作效率有大幅度的提高.开发Android也是如此,大家可能都离不开Eclipse或Android Studio这些工具,但他们 ...
- HTML5本地存储之localStorage、sessionStorage
1.概述 localStorage和sessionStorage统称为Web Storage,它使得网页可以在浏览器端储存数据. sessionStorage保存的数据用于浏览器的一次会话,当会话结束 ...
- EPLAN Electric P8 2.0即将到来,着实令人期待-转caodaping
在今年的4月份,2.0版本的EPLAN Electric P8 首次揭开其神秘面纱,见诸于世.它的展露,再次印证了EPLAN 软件平台朝着"更实用"这一方向发展,同时也证明&quo ...
- [LeetCode] Sparse Matrix Multiplication
Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...
- 理解WebSocket
WebSocket的动机是什么? 目前的Web通信使用的是HTTP协议,HTTP协议是基于TCP协议的应用层协议,HTTP协议的工作模式是request/response模式.在一次通信中,必须首先由 ...
- Mvc 拼接Html 导出 Excel(服务器不用安装呦!支持2007以上版本)
新公司,新接触,老方法,更实用. 之前接触过Webform,winfrom 的导出Excel方法 ,优点:省事.缺点:服务器必须安装Office 这几天做项目 和 大牛学习了一下 新的方法,自己加以总 ...
- mysql日期类型默认值'0000-00-00'容错处理
mysql日期默认值'0000-00-00'惹的祸 .net连mysql数据库时,如果表里有字段是日期型且值是‘0000-00-00’时,会报错.在C#里面日期不可能是那样的.或许是最小日期定义的差别 ...
- C、C++编译,链接,extern链接
//b.cpp #inlcude <iostream> void b() { std::cout<<"fun b"; } //a.cpp extern vo ...