Fluent NHibernate and Mysql, SQLite, PostgreSQL, and nhibernatesqlite

Source: Internet
Author: User

Fluent NHibernate and Mysql, SQLite, PostgreSQL, and nhibernatesqlite

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 testDROP 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 creates an ISessionFactory // </summary> /// <returns> </returns> public static ISessionFactory GetSessionFactory () {if (_ sessionFactory = null) {lock (_ objLock) {if (_ sessionFactory = null) {// configure ISessionFactory _ sessionFactory = fluentnhib.pdf. cfg. fluently. configure () // Configure the database. database (fluentnhib.pdf. 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 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 (problems still exist in the ISessionFactory test)

 /// <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:

--PostgreSQLdrop 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 = s; Password = 770214; "; ISessionFactory sessionFactory = Fluently. configure (). database (PostgreSQLConfiguration. standard. connectionString (connectionStr ). showSql ()). mappings (m => m. fluentMappings // AddFromAssemblyOf <FluentNHibernateHelper> () // TypeOfFluentNHibernateMapping. addFromAssembly (Assembly. getExecutingAssembly () // Usage Note //. exposeConfiguration (CreateSchema ). buildSessionFactory () ;}} return _ sessionFactory ;}

  

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.