NHibernate: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
==App Settings==
<source lang="bash">
{
  "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"
  }
}
</source>
==NHibernate Config==
<source lang="xml">
<?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>
</source>
==NHibernate Helper==
<source lang="csharp">
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);
        }
    }
}
</source>
==References==
==References==
{|
{|
Line 14: Line 86:


| valign="top" |
| valign="top" |
* [https://www.codeproject.com/Questions/178312/Could-not-load-file-or-assembly-Oracle-DataAccess Could not load file or assembly <code>Oracle.DataAccess</code>]
* [https://stackoverflow.com/questions/27273642/ An invalid or Incomplete config used for SessionFactory]
* [https://stackoverflow.com/questions/27273642/ An invalid or Incomplete config used for SessionFactory]
* [https://stackoverflow.com/questions/189280/ NHibernate with SQLite Memory & Shared Cache]
* [https://stackoverflow.com/questions/33509331/ <code>Oracle.DataAccess.Client</code> Dependencies]
* [https://stackoverflow.com/questions/15370001/ Setting arbitrary properties in Fluent NHibernate]
* [https://medium.com/@RobertKhou/asp-net-core-mvc-identity-using-postgresql-database-bc52255f67c4 ASP.NET Core MVC Identity using PostgreSQL]
* [https://stackoverflow.com/questions/32229132/ NHibernate & Oracle Managed Client]
* [https://www.dotnetjalps.com/2014/07/fluent-nhibernate-asp-net-mvc-crud.html ASP.NET MVC & Fluent Nhibernate]
* [https://www.dotnetjalps.com/2014/07/fluent-nhibernate-asp-net-mvc-crud.html ASP.NET MVC & Fluent Nhibernate]
* [https://ivanderevianko.com/2015/04/vnext-use-postgresql-fluent-nhibernate-from-asp-net-5-dnx-on-ubuntu Fluent NHibernate with PostgreSQL]
* [https://ivanderevianko.com/2015/04/vnext-use-postgresql-fluent-nhibernate-from-asp-net-5-dnx-on-ubuntu Fluent NHibernate with PostgreSQL]
* [https://github.com/auth0-blog/dependency-injection-dotnet-core Dependency Injection in .NET Core]
| valign="top" |
* [https://auth0.com/blog/dependency-injection-in-dotnet-core Understanding Dependency Injection in .NET Core]
* [https://codeburst.io/jwt-auth-in-asp-net-core-148fb72bed03 JWT Auth in ASP.NET Core]
|}
----
{|
| valign="top" |
| valign="top" |
|}
|}

Latest revision as of 00:52, 30 December 2020

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