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的更多相关文章

  1. 关于PDF.NET开发框架对Mysql Sqlite PostgreSQL数据库分页支持的个人看法

    关于PDF.NET开发框架的名字由来  在设计www.pwmis.com站点的时候,考虑到架构的兼容性和将来升级的可能性,最重要的是没有足够的时间去为网站添加和维护很多复杂的程序,所以在借鉴前人成功经 ...

  2. SQLite vs MySQL vs PostgreSQL:关系型数据库比较

    自1970年埃德加·科德提出关系模型之后,关系型数据库便开始出现,经过了40多年的演化,如今的关系型数据库种类繁多,功能强大,使用广泛.面对如此之多的关系型数据库,我们应该如何权衡找出适合自己应用场景 ...

  3. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

  4. Fluent Nhibernate之旅(五)--利用AutoMapping进行简单开发

    Fluent Nhibernate(以下简称FN)发展到如今,已经相当成熟了,在Nhibernate的书中也相应的推荐了使用FN来进行映射配置,之前写的FN之旅至今还有很多人会来私信我问题,说来惭愧, ...

  5. 对Oracle 、SQL Server、MySQL、PostgreSQL数据库优缺点分析

    对Oracle .SQL Server.MySQL.PostgreSQL数据库优缺点分析 Oracle Database Oracle Database,又名Oracle RDBMS,或简称Oracl ...

  6. Redis/Mysql/SQLite/MongoDB 数据库对比

    一.Redis: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...

  7. 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)

    在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...

  8. [Fluent NHibernate]第一个程序

    目录 写在前面 Fluent Nhibernate简介 基本配置 总结 写在前面 在耗时两月,NHibernate系列出炉这篇文章中,很多园友说了Fluent Nhibernate的东东,也激起我的兴 ...

  9. [Fluent NHibernate]一对多关系处理

    目录 写在前面 系列文章 一对多关系 总结 写在前面 上篇文章简单介绍了,Fluent Nhibernate使用代码的方式生成Nhibernate的配置文件,以及如何生成持久化类的映射文件.通过上篇的 ...

随机推荐

  1. Jenkins FTP 上传

    需要插件:FTP publisher plugin 进入 Jenkins / 系统管理 / 系统设置 找到 FTP repository hosts,新增一个,编辑好,保存 打开 Jenkins / ...

  2. ffmpeg安装的问题

    php语音转换需要安装ffmpeg文件 参考地址: http://thierry-xing.iteye.com/blog/2017864 http://diogomelo.net/blog/11/en ...

  3. CentOS6下Haproxy的安装配置

    Haproxy 是一个开源的负载均衡和反向代理软件,其提供了高可用的网络服务.其一般是应用于web服务,但同时也能为SMTP和终端服务等提供可靠的支持. 1.下载安装haproxy wget ftp: ...

  4. win7搭建ios开发环境

    安装过程参考文章: http://jingyan.baidu.com/article/ff411625b9011212e48237b4.html http://www.loukit.com/threa ...

  5. 创建 iPhone/iOS8 弹出菜单(窗口)

    基本步骤 添加视图:主视图与弹出视图 关联视图 配置弹出视图 编码实现:弹出菜单样式及控制器委托 override func prepareForSegue(segue: UIStoryboardSe ...

  6. mycat配置日志

    1: 1: MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' ...

  7. Devexpress treelist 树形控件 实现带三种状态的CheckBox

    树形控件是使用频率很高的一种控件.对于属性控件往往需要下面两个功能 1.TreeList带有CheckBox,并且节点要有三种状态(所有的子节点都选中,所有的子节点都没选择,一部分子节点选中).使用 ...

  8. slf4j日志系统

    slf4j:由外观模式实现,不是日志的解决方案,只是服务于各种各样的日志系统.可以让在应用部署的时候,选择合适的日志系统 slf4j + log4j : 配置日志文件:log4j.properties ...

  9. datagrid 动态列

    var options={}; $(function(){ var myNj = 9; //初始化 $("#disgrid").datagrid({ type: 'POST', n ...

  10. 【转】mysql如何跟踪执行的sql语句

    转自http://blog.csdn.net/testcs_dn/article/details/18791815 在SQL SERVER下跟踪sql采用事件探查器,而在mysql下如何跟踪sql呢? ...