我想使用新的VS2010 web.config转换特性来更改我的web.config文件中nhibernate配置中的连接字符串。相关的代码片段如下所示:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
<property name="connection.connection_string">(test connection string)</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
...我尝试了以下转换,但没有成功:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.connection_string" xdt:Transform="Replace">(production connection string)</property>
</session-factory>
</hibernate-configuration>
</configuration>问题似乎出在nhibernate-configuration元素的xmlns属性中。
在部署期间,将(测试连接字符串)替换为(生产连接字符串)应该是什么正确的转换?
发布于 2010-09-22 07:42:23
答案可能有点晚了,但既然我也需要这个,我想我会发布一个对我有效的答案,以防其他人遇到这个问题。
需要结合使用xdt:Locator和xpath表达式来获得正确的节点。所以像这样的东西应该是可行的。
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.connection_string" xdt:Locator="XPath(//*[local-name()='hibernate-configuration']//*[local-name()='property'][@name='connection.connection_string'])" xdt:Transform="Replace">(production connection string)</property>
</session-factory>
</hibernate-configuration>可能有更好的xpath表达式,但这就是对我有效的方法。
唯一的问题是,被替换的节点将在该节点上重新声明一个名称空间。因此,被替换的节点在最终输出中实际上将如下所示。
<property name="connection.connection_string" xmlns="urn:nhibernate-configuration-2.2">(production connection string)</property>发布于 2010-09-27 14:28:30
我最近遇到了同样的问题-通过在转换文件中放置显式名称空间前缀解决了这个问题
<configuration
xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
xmlns:hib="urn:nhibernate-configuration-2.2"
>
<hib:hibernate-configuration>
<hib:session-factory>
<hib:property name="connection.connection_string" xdt:Transform="Replace">(production connection string)</hib:property>
</hib:session-factory>
</hib:hibernate-configuration>
</configuration>谢天谢地,转换后的web.config文件没有名称空间前缀(即,它将nhibernate名称空间声明保留在与原始web.config文件相同的位置,并正确命名了所有节点)。
发布于 2012-08-22 22:22:18
如果您要做的全部工作就是转换连接字符串,请不要使用转换机制。相反,应在web.config或app.config中引用此属性
connection.connection_string_name而不是这个:
connection.connection_string这允许您引用ConnectionStrings部分中定义的连接字符串,该字符串将以通常的方式进行转换。
例如,在web.config中,使用以下代码:
<connectionStrings>
<add name="DefaultConnection" connectionString="server=MYSERVER; Integrated Security=SSPI; database=MYDATABASE"/>
</connectionStrings>
<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_name">DefaultConnection</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<property name="current_session_context_class">web</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>https://stackoverflow.com/questions/3062636
复制相似问题