为什么我无法覆盖 hibernate.cfg.xml 中的配置

Why I can't override configuration in hibernate.cfg.xml

我正在逐步完成教程 NHibernate - Override Configuration,但不幸的是,我在 Program.cs 的最后一行遇到错误:

"The format of the initialization string does not match the specification starting with index 0."

错误更深:

   in System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
   in System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
   in System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
   in System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
   in System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
   in System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
   in System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
   in NHibernate.Connection.DriverConnectionProvider.GetConnection()
   in NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare()
   in NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(Dialect dialect, IConnectionHelper connectionHelper)
   in NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactoryImplementor sessionFactory)
   in NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
   in NHibernate.Cfg.Configuration.BuildSessionFactory()
   in NHibernateDataTypesMapping.Program.Main(String[] args) w C:\...\NHibernateLab\NHibernateDataTypesMapping\Program.cs:line 29

Program.cs:

class Program
    {
        static void Main(string[] args)
        {
            var cfg = new Configuration();
            cfg.DataBaseIntegration(x => {
                x.LogSqlInConsole = true;
            });
            cfg.Configure();
            var sefact = cfg.BuildSessionFactory();
        }
    }

hibernate.cfg.xml:

<?xml version = "1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>

    <property name="connection.connection_string">default</property>
    <!--<property name="connection.connection_string">
      Data Source=(localdb)\MSSQLLocalDB;
      Initial Catalog=NHibernateLabDB;
      Integrated Security=True;
      Connect Timeout=30;
      Encrypt=False;
      TrustServerCertificate=False;
      ApplicationIntent=ReadWrite;
      MultiSubnetFailover=False
    </property>-->

    <property name="connection.driver_class">
      NHibernate.Driver.SqlClientDriver
    </property>
    <property name="dialect">
      NHibernate.Dialect.MsSql2008Dialect
    </property>
    <mapping assembly="NHibernateDataTypesMapping"/>
  </session-factory>
</hibernate-configuration>

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  <connectionStrings>
    <add name="default" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=NHibernateLabDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>
  </connectionStrings>
</configuration>

当我在 hibernate.cfg.xml 中使用包含连接字符串的 属性 时一切正常,我可以在数据库上工作,但是当我尝试在 App.config 中使用连接字符串时出现错误。 我将不胜感激。

如此处所述

3.4. NHibernate provided ADO.NET connection

connection.connection_string Connection string to use to obtain the connection. connection.connection_string_name The name of the connection string (defined in <connectionStrings> configuration file element) to use to obtain the connection.

...在这种情况下不同key/name。 IE。而不是那个:

<property name="connection.connection_string">default</property>

我们需要这个:

<property name="connection.connection_string_name">default</property>