当前位置:Gxlcms > 数据库问题 > Fluent NHibernate and Mysql,SQLite

Fluent NHibernate and Mysql,SQLite

时间:2021-07-01 10:21:17 帮助过:2人阅读

http://codeofrob.com/entries/sqlite-csharp-and-nhibernate.html

https://code.google.com/archive/p/csharp-sqlite/downloads

https://github.com/davybrion/NHibernateWorkshop

MySQL

  1. /// <summary>
  2. ///MySQL 创建ISessionFactory
  3. /// </summary>
  4. /// <returns></returns>
  5. public static ISessionFactory GetSessionFactory()
  6. {
  7. if (_sessionFactory == null)
  8. {
  9. lock (_objLock)
  10. {
  11. if (_sessionFactory == null)
  12. {
  13. //配置ISessionFactory
  14. _sessionFactory = FluentNHibernate.Cfg.Fluently.Configure()
  15. //数据库配置
  16. .Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard
  17. .ConnectionString(c=>c.Server("")
  18. .Database("geovindu")
  19. .Password("520")
  20. .Username("root"))
  21. )
  22. .Mappings(m => m
  23. //.FluentMappings.PersistenceModel
  24. //.FluentMappings.AddFromAssembly();
  25. .FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())) //用法注意
  26. .BuildSessionFactory();
  27. // Fluently.Configure().Database(
  28. // MySqlConfiguration.Standard.ConnectionString(
  29. // c => c.FromConnectionStringWithKey("ConnectionString")
  30. // )
  31. //)
  32. //.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyAutofacModule>())
  33. //.BuildSessionFactory())
  34. }
  35. }
  36. }
  37. return _sessionFactory;
  38. }
  39. /// <summary>
  40. /// 重置Session
  41. /// </summary>
  42. /// <returns></returns>
  43. public static ISession ResetSession()
  44. {
  45. if (_session.IsOpen)
  46. _session.Close();
  47. _session = _sessionFactory.OpenSession();
  48. return _session;
  49. }
  50. /// <summary>
  51. /// 打开ISession
  52. /// </summary>
  53. /// <returns></returns>
  54. public static ISession GetSession()
  55. {
  56. GetSessionFactory();
  57. if (_session == null)
  58. {
  59. lock (_objLock)
  60. {
  61. if (_session == null)
  62. {
  63. _session = _sessionFactory.OpenSession();
  64. }
  65. }
  66. }
  67. return _session;
  68. }

  SQLite (测试ISessionFactory还存在问题)

  1. /// <summary>
  2. /// http://frankdecaire.blogspot.com/2014/04/fluent-nhibernate-unit-testing-using.html
  3. /// </summary>
  4. public static class SQLLiteSessionFactory
  5. {
  6. private static ISessionFactory _sessionFactory;
  7. private static ISessionFactory SessionFactory
  8. {
  9. get
  10. {
  11. if (_sessionFactory == null)
  12. {
  13. _sessionFactory = Fluently.Configure()
  14. .Database(SQLiteConfiguration
  15. .Standard
  16. .InMemory()
  17. .UsingFile("sibodu.db")
  18. )
  19. .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateTestProject.Entites.Department>())
  20. .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateTestProject.Entites.Employee>())
  21. .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
  22. .BuildSessionFactory();
  23. }
  24. return _sessionFactory;
  25. }
  26. }
  27. public static ISession OpenSession()
  28. {
  29. return SessionFactory.OpenSession();
  30. }
  31. }

  

/// 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. /// <summary>
  2. /// Nhibernate
  3. /// </summary>
  4. public class MonoSQLiteDriver : NHibernate.Driver.ReflectionBasedDriver
  5. {
  6. public MonoSQLiteDriver()
  7. : base(
  8. "Mono.Data.Sqlite",
  9. "Mono.Data.Sqlite",
  10. "Mono.Data.Sqlite.SqliteConnection",
  11. "Mono.Data.Sqlite.SqliteCommand")
  12. {
  13. }
  14. public override bool UseNamedPrefixInParameter
  15. {
  16. get
  17. {
  18. return true;
  19. }
  20. }
  21. public override bool UseNamedPrefixInSql
  22. {
  23. get
  24. {
  25. return true;
  26. }
  27. }
  28. public override string NamedPrefix
  29. {
  30. get
  31. {
  32. return "@";
  33. }
  34. }
  35. public override bool SupportsMultipleOpenReaders
  36. {
  37. get
  38. {
  39. return false;
  40. }
  41. }
  42. }
  43. /// <summary>
  44. /// Fluent NHibernate
  45. ///
  46. /// </summary>
  47. public class MonoSQLiteConfiguration : PersistenceConfiguration<MonoSQLiteConfiguration>
  48. {
  49. public static MonoSQLiteConfiguration Standard
  50. {
  51. get { return new MonoSQLiteConfiguration(); }
  52. }
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. public MonoSQLiteConfiguration()
  57. {
  58. Driver<MonoSQLiteDriver>();
  59. Dialect<SQLiteDialect>();
  60. Raw("query.substitutions", "true=1;false=0");
  61. }
  62. /// <summary>
  63. ///
  64. /// </summary>
  65. /// <returns></returns>
  66. public MonoSQLiteConfiguration InMemory()
  67. {
  68. Raw("connection.release_mode", "on_close");
  69. return ConnectionString(c => c
  70. .Is("Data Source=:memory:;Version=3;"));//New=True;
  71. }
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. /// <param name="fileName"></param>
  76. /// <returns></returns>
  77. public MonoSQLiteConfiguration UsingFile(string fileName)
  78. {
  79. return ConnectionString(c => c
  80. .Is(string.Format("Data Source={0};Version=3;Pooling=true;FailIfMissing=false;UTF8Encoding=True;", fileName)));//New=True;
  81. }
  82. /// <summary>
  83. ///
  84. /// </summary>
  85. /// <param name="fileName"></param>
  86. /// <param name="password"></param>
  87. /// <returns></returns>
  88. public MonoSQLiteConfiguration UsingFileWithPassword(string fileName, string password)
  89. {
  90. return ConnectionString(c => c
  91. .Is(string.Format("Data Source={0};Version=3;New=True;Password={1};", fileName, password)));
  92. }
  93. }

  

Fluent NHibernate and Mysql,SQLite

标签:

人气教程排行