转换Web.config Hibernate配置VS 2012

时间:2013-07-17 11:52:27

标签: c# hibernate visual-studio-2012 web-config

我有Web.config:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/>
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
      <property name="connection.connection_string">{old_connection}</property>
      <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
      <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate</property>
      <property name="show_sql">false</property>
      <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    </session-factory>
  </hibernate-configuration>
</configuration>

我应用转换。 Web.Release.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
               xmlns:hib="urn:nhibernate-configuration-2.2">
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/>
  </configSections>
  <hib:hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <hib:session-factory>
      <hib:property xdt:Transform="Replace" xdt:Locator="Match(name)" name="connection.connection_string">{new_connection}</hib:property>
    </hib:session-factory>
  </hib:hibernate-configuration>
</configuration>

在Release中运行VS2012,不会发生转换。字符串未被替换。 可能是什么原因?

1 个答案:

答案 0 :(得分:2)

由于元素名称与基线Web.config不同,因此不会发生转换。如果删除hib命名空间,则会进行转换。

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property xdt:Transform="Replace" xdt:Locator="Match(name)" name="connection.connection_string">{new_connection}</property>
    </session-factory>
  </hibernate-configuration>
</configuration>

另一件需要注意的事情。如果您当前的配置为Release且您通过Visual Studio运行应用程序,则服务器将指向项目的根目录。在根目录中,您将拥有Web.configWeb.Debug.configWeb.Release.config,服务器将获取通常的配置文件,而不进行转换(即Web.config)。