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

时间:2019-05-08 18:59:18

标签: c# .net xml nhibernate

我正在逐步进行教程NHibernate - Override Configuration,但是很遗憾,我在 Program.cs 的最后一行中遇到了错误:

  

“初始化字符串的格式与从索引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 中使用包含连接字符串的属性时,一切正常,我可以在db上工作,但是当我尝试在中使用连接字符串时App.config 我遇到了错误。 我将不胜感激。

1 个答案:

答案 0 :(得分:0)

如此处记录

3.4. NHibernate provided ADO.NET connection

  

connection.connection_string用于获取连接的连接字符串。   connection.connection_string_name连接字符串的名称​​(在<connectionStrings>配置文件元素中定义),用于获取连接。

...在这种情况下,其他键/名称。即而不是:

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

我们需要这个:

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