NHibernate

From Chorke Wiki
Jump to navigation Jump to search

App Settings

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "SqlServer": "Data Source=(localdb)\\MSSQLLocalDB;Database=hibernate;Integrated Security=True",
    "Oracle": "Data Source=iis0.dev.shahed.biz:1521/xe;User Id=shahed_boot_dev;Password=shahed_boot_dev",
    "MySQL": "Server=db00.dev.shahed.biz;Port=3306;Database=shahed_boot_dev;User Id=shahed_boot_dev;Password=shahed_boot_dev",
    "PostgreSQL": "Server=db00.dev.shahed.biz;Port=5432;Database=shahed_boot_dev;User Id=shahed_boot_dev;Password=shahed_boot_dev"
  }
}

NHibernate Config

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.driver_class">NHibernate.Driver.OracleManagedDataClientDriver</property>
    <property name="connection.connection_string">Data Source=iis0.dev.shahed.biz:1521/xe;User Id=shahed_boot_dev;Password=shahed_boot_dev</property>
    <property name="show_sql">true</property>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
  </session-factory>
</hibernate-configuration>

NHibernate Helper

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using ChorkeAcademiaPersis.Entity;
using Microsoft.Extensions.Configuration;
using NHibernate.Cfg;

namespace ChorkeAcademiaPersis.Utility
{
    public class NHibernateHelper
    {
        public static ISession OpenSession()
        {
            ISessionFactory sessionFactory = Fluently.Configure()
                .Database(MySQLConfiguration.Standard.ConnectionString(Startup.StaticConfig.GetConnectionString("MySQL")).ShowSql())
                //.Database(MsSqlConfiguration.MsSql2012.ConnectionString(Startup.StaticConfig.GetConnectionString("SqlServer")).ShowSql())
                //.Database(PostgreSQLConfiguration.Standard.ConnectionString(Startup.StaticConfig.GetConnectionString("PostgreSQL")).ShowSql())
                //.Database(OracleDataClientConfiguration.Oracle10.ConnectionString(Startup.StaticConfig.GetConnectionString("Oracle")).ShowSql())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Employee>())
                .ExposeConfiguration(ConfigModifier)
                .BuildSessionFactory();

            return sessionFactory.OpenSession();
        }

        private static void ConfigModifier(Configuration config)
        {
            //config.SetProperty(Environment.ConnectionDriver, "NHibernate.Driver.OracleManagedDataClientDriver");
            //config.SetProperty(Environment.QuerySubstitutions, "true 1, false 0, yes 'Y', no 'N'");
            //config.SetProperty(Environment.CurrentSessionContextClass, "thread_static");
            var schemaExport = new SchemaExport(config);
            schemaExport.Create(false, false);
        }
    }
}

References