如何将 hibernate.cfg 移动到 appsettings.json

How to I move hibernate.cfg into appsettings.json

对于 NHibernate 和 .NET Core,我有一个 hibernate.cfg.xml 文件定义为:

<?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. 将它放入我的 appsettings.json 文件的语法是什么?
  2. 加载上述 json 文件的语法是什么?

或者,是否可以只将连接字符串移动到 appsettings.json?这是我最需要控制的。

正如您在评论中提到的,通过代码配置也是您可以接受的解决方案。以下是如何通过代码配置 NHibernate 会话工厂:

导入命名空间:

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

配置代码如下:

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();

对于 AddMapping 方法,您需要提供一个 Assembly,您在其中定义了映射。

在应用程序启动时调用此代码一次。在整个应用程序生命周期内维护 sessionFactory 的实例以创建 ISession.

另外,如果要配置log4net进行日志记录,请参考this回答。

一个选项是给 Nhibernate 配置一个 IDictionnary<string, string>。它是 Microsoft.Extensions.Configuration.IConfiguration 和 Nhibernate 配置之间的共同点。

例如,如果您的 Nhibernate 设置位于 appsettings.json 的 NHibernate 部分:

{
  "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
  }
}

然后你可以像那样构建你的 ISessionFactory :

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();