Fluent NHibernate and Mysql,sqlite

Source: Internet
Author: User
Tags static class

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

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

https://github.com/davybrion/NHibernateWorkshop

MySQL

  /// <summary>
         /// MySQL creates ISessionFactory
         /// </ summary>
         /// <returns> </ returns>
         public static ISessionFactory GetSessionFactory ()
         {
             if (_sessionFactory == null)
             {
                 lock (_objLock)
                 {
                     if (_sessionFactory == null)
                     {
                         // Configure ISessionFactory
                         _sessionFactory = FluentNHibernate.Cfg.Fluently.Configure ()
                             // Database configuration
                        .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 ())) // Usage note
                     .BuildSessionFactory ();
                        

                    // Fluently.Configure (). Database (
                    // MySqlConfiguration.Standard.ConnectionString (
                    // c => c.FromConnectionStringWithKey ("ConnectionString")
                    //)
                    //)
                    //.Mappings(m => m.FluentMappings.AddFromAssemblyOf <MyAutofacModule> ())
                    //. BuildSessionFactory ())

                     }
                 }
             }
             return _sessionFactory;
 
         }
         /// <summary>
         /// Reset Session
         /// </ summary>
         /// <returns> </ returns>
         public static ISession ResetSession ()
         {
             if (_session.IsOpen)
                 _session.Close ();
             _session = _sessionFactory.OpenSession ();
             return _session;
         }
         /// <summary>
         /// Open ISession
         /// </ summary>
         /// <returns> </ returns>
         public static ISession GetSession ()
         {
              GetSessionFactory ();
             if (_session == null)
             {
                 lock (_objLock)
                 {
                     if (_session == null)
                     {
                         _session = _sessionFactory.OpenSession ();
                     }
                 }
             }
             return _session;
         }
SQLite (There are still problems in testing 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)));
        }
    }


Fluent NHibernate and Mysql, SQLite

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.