如何在 web.config 中声明或使用变量

How do you declare or use variables in web.config

因为我们的 Web 应用程序同时使用 Entityframework 和 System.Data.SqlClient,我们需要有 2 个。 这很容易出错,因为您需要用相同的数据更新两者。

所以我想知道是否可以向 web.config xml 添加变量?

<connectionStrings>
   <add name="Server" value="sql2014" type="variable" />
   <add name="database" value="MyDB" type="variable" />
   <add name="userName" value="sqluser" type="variable" />
   <add name="password" value="kaldsommer" type="variable" />

    <add name="XReport" connectionString="Data Source={Server};Initial Catalog={MyDB};User ID={username};Password={password}" providerName="System.Data.SqlClient" />
    <add name="XEntities" connectionString="metadata=res://*/Entities.XModel.csdl|res://*/Entities.XModel.ssdl|res://*/Entities.XModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source{Server};initial catalog={MyDB};MultipleActiveResultSets=True;App=EntityFramework;;Persist Security Info=True;User ID={username};Password={password}&quot;" providerName="System.Data.EntityClient" /> 
</connectionStrings>

Pr @Bartude 在评论中回复。

FAIK, this is not possible. On the other hand maintaining 2 connection strings is not that complicated, although I agree it's always best to update things only once