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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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)

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/// <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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
--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还存在问题)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// <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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/// <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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/// <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;
 
     }     

  

posted @   ®Geovin Du Dream Park™  阅读(862)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2015-03-31 Access MetaData
2011-03-31 c# asp.net page head,meta,title,css
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示