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. INSTALL MYSQL IN MAC

    安装好MYSQL后,在System References中找到MYSQL,启动它. 启动之后在终端中输入添加MySQL路径的命令,把MYSQL路径添加到PATH中: PATH="$PATH& ...

  2. websocket for python

    https://github.com/aaugustin/websockets server.py #!/usr/bin/env python import asyncioimport websock ...

  3. 从Tmux 转到GNU Screen

    网上很多地方都说Tmux比GNU Screen要好用,不过无意间看到这篇Switching from tmux to GNU Screen之后,我发现GNU Screen的窗口/区域概念更好,至少是更 ...

  4. ubuntu-15.04-server-i386.iso 安装 Oracle 11gR2 数据库

    特点: 需要重新安装老版本的 libaio1_0.3.109-2ubuntu?_i386.deb.默认的libaio库有问题,和其默认libaio的编译方式有关! 默认的gcc 4.9 需要使用 -W ...

  5. Android 中ViewPagerIndicator的使用

    1.https://github.com/JakeWharton/Android-ViewPagerIndicator 2.http://blog.csdn.net/xiaanming/article ...

  6. 修复AWS上EC2损坏的sshd_config文件

    常识: AWS是没有root用户的,登陆也都是通过SSH KEY完成授权认证. 背景: 正在AWS上搭一个CI (GO),与gitlab,为了将其进行集成,需将gitlab的deploy key设置成 ...

  7. [Python] Magic editor in Pycharm

    From: http://blog.csdn.net/u013088062/article/details/50249751 From:http://blog.csdn.net/u013088062/ ...

  8. Spring Remoting: Burlap--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the ...

  9. Hadoop入门进阶课程13--Chukwa介绍与安装部署

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan  ...

  10. Hadoop入门进阶课程8--Hive介绍和安装部署

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan  ...