2

For NHibernate and .NET Core, I have a hibernate.cfg.xml file defined as:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=.;Database=OrmCookbook;Trusted_Connection=True;</property>

    <property name="show_sql">false</property>
  </session-factory>
</hibernate-configuration>
  1. What is the syntax for putting it in my appsettings.json file?
  2. What is the syntax for loading said json file?

Alternately, if it possible to move just the connection string into appsettings.json? That's the one I need to control the most.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447

2 Answers2

1

As you mentioned in comment, configuration by code is also acceptable solution to you. Following is how you can configure NHibernate Session Factory by code:

Import the namespaces:

using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;

Following is the configuration code:

Configuration configuration = new Configuration();

configuration.SetProperty(NHibernate.Cfg.Environment.Dialect, "YourDialect");
configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionString, "YourConnectionString");
configuration.SetProperty(NHibernate.Cfg.Environment.DefaultSchema, "YourDefaultSchema");
configuration.SetProperty(NHibernate.Cfg.Environment.Isolation, "ReadCommitted");
configuration.SetProperty(NHibernate.Cfg.Environment.BatchSize, "YourBatchSize");
configuration.SetProperty(NHibernate.Cfg.Environment.ShowSql, "true");
configuration.SetProperty(NHibernate.Cfg.Environment.FormatSql, "true");
configuration.AddMapping(YourHbmMappingInstance);
configuration.SessionFactory().DefaultFlushMode("YourFlushMode");

ISessionFactory sessionFactory = configuration.BuildSessionFactory();

For AddMapping method, you need to provide an Assembly in which you have defined your mappings.

Call this code once at the startup of application. Maintain instance of sessionFactory throughout the application life time to create ISession.

Further, if you want to configure log4net for logging, please refer to this answer.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • 1
    I ended up just using the hibernate.cfg.xml file and just overwrote the connection string with `configuration.SetProperty`. – Jonathan Allen Jan 25 '20 at 01:32
0

An option is to give a IDictionnary<string, string> to the Nhibernate configuration. It is the common denominator between Microsoft.Extensions.Configuration.IConfiguration and Nhibernate configuration.

For instance, if your Nhibernate settings are in a NHibernate section in the appsettings.json :

{
  "ConnectionStrings":{
    "myconnstring":"..."
  },
  "NHibernate": {
    "connection.connection_string_name":"myconnstring",
    "dialect": "NHibernate.Dialect.PostgreSQL83Dialect",
    "current_session_context_class": "async_local",
    "cache.use_second_level_cache": true,
    "transaction.use_connection_on_system_prepare": false,
    "cache.provider_class": "NHibernate.Cache.HashtableCacheProvider, NHibernate",
    "cache.use_query_cache": false
  }
}

Then you can build your ISessionFactory like that :

IDictionary<string, string> nHibernateSettings = config.GetSection("NHibernate")
    .GetChildren()
    .ToDictionary(x => x.Key, y => y.Value);

if(nHibernateSettings.TryGetValue(NHibernate.Cfg.Environment.ConnectionStringName, out string? connStringName) && !string.IsNullOrWhiteSpace(connStringName))
{
    nHibernateSettings[NHibernate.Cfg.Environment.ConnectionString] = config.GetConnectionString(connStringName);
}

var conf = new NHibernate.Cfg.Configuration() { Properties = nHibernateSettings };
conf.AddAssembly("your assembly name");
ISessionFactory sf = sessionFactory = conf.BuildSessionFactory();
Gengis
  • 31
  • 1
  • 2