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; }

  

最新文章

  1. Angular-Chart.js 初接触;;;
  2. 关于Dijkstra最短路径算法
  3. Does the OpenSceneGraph have a native file format?
  4. javascript对象转化为基本数据类型规则
  5. 《more effective C++》条款10 防止构造函数里的资源泄露
  6. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.5.安装Grid,创建ASM磁盘组空间不足
  7. jpa-hibernate配置转换
  8. Android 二维码扫描与生成
  9. 练习C之SELECT形式的非阻塞IO
  10. HTTP协议探析
  11. C++数组与指针
  12. CSS学习笔记2:伪类
  13. 《Linux就该这么学》第十九天课程
  14. SpringBoot学习(二)--&gt;Spring的Java配置方式
  15. C#实现向excel中插入行列,以及设置单元格合并居中效果
  16. Ncurses - Panel
  17. js--函数声明和函数表达式--执行顺序
  18. VUE2.0 饿了吗视频学习笔记(四):颜色、跳转、设置、vue-resource
  19. shell 变量介绍
  20. laravel 5.1部署到 集成环境 lnmp上

热门文章

  1. (笔记)Linux内核学习(七)之内核同步机制和实现方式
  2. MYSQL 的一些文件及说明
  3. 单线程vs多线程
  4. wndows系统命令总结
  5. 【Xamarin报错】 COMPILETODALVIK : UNEXPECTED TOP-LEVEL error java.lang.OutOfMemoryError: Java heap space
  6. IOS UITableView下拉刷新和上拉加载功能的实现
  7. 生成证书时Distribution下面App Store and Ad Hoc 选项不能选择的原因及解决办法
  8. Django数据模型及操作
  9. ubuntu使用
  10. 使用JavaScript判断用户是否为手机设备