Web.config Remove ConnectionStrings node from Web.Release.Config - visual-studio

I have this in my Web.config so that my service will run locally:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=IPADD;Initial Catalog=DB;Persist Security Info=True;User ID=UN;Password=PW" />
</connectionStrings>
I need this here because the Web.Debug.Config doesn't work. So when I publish I want to remove this string because it is actually inherited from a parent Web.config
<connectionStrings>
<remove name="DefaultConnection"/>
</connectionStrings>
This doesn't seem to work... (it throws a The entry 'DefaultConnection' has already been added. error)
How can I remove it? XSLT?
P.s. I can't use clear/ because it removes the Machine.Config DB Connection string to the Membership provider also throwing a different error

Put below code:
<connectionStrings>
<add name="LocalizationDB"
xdt:Transform="Remove" xdt:Locator="Match(name)"/>
</connectionStrings>
But be careful when you do publish from visual studio. When you look on Output view. It should write you something like:
Transformed Web.config using Web.Release.config into obj\Debug\TransformWebConfig\transformed\Web.config.
Copying all files to temporary location below for package/publish:
But if you have active "Debug" you will have Web.Debug.config instead of Web.Release.config

Related

Dev Database connection persisting on production environment

as the title suggests i'm having a little problem with a persisting database connection. Here, we have 2 databases, one for dev and testing and the oficial, production one. We used to change manually the different connection strings when testing and deploying and sometimes a strange error happened. When we stopped the dev database suddenly the entire production application stopped to work, saying that the SQL connection was lost. But the web.config was pointing to production not testing environment!
After some research, we found the web.config transformation and thought that would solve our problem. It didn't. The problem still persists as if our application is tight to the dev environment even when our web.config doesn't make a single reference to it.
To make things clearer, i'll post the connection strings, the transformation and the latest SQL error that happened (when we limited the dev database to single-user for some updates), plus the way we connect it to our DBML (L2SQL).
CONNECTION STRINGs
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
<!--ConnectionString Central-->
<add name="OperationsBD" connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=OPERATION;Persist Security Info=True;User ID=lab;Password=devPassword" providerName="System.Data.SqlClient"/>
<add name="AnalisysBD" connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=lab;Password=devPassword" providerName="System.Data.SqlClient"/>
<!--Production ConnectionString-->
<!--<add name="OperationsDB" connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=OPERATION;Persist Security Info=True;User ID=company;Password=prodPassword" providerName="System.Data.SqlClient"/>
<add name="AnalisysDB" connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=company;Password=prodPassword" providerName="System.Data.SqlClient"/>-->
WEB.CONFIG TRANSFORMATION (PRODUCTION)
<connectionStrings>
<add name="OperationsDB"
connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=OPERATION;Persist Security Info=True;User ID=company;Password=prodPassword"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="AnalisysDB"
connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=company;Password=prodPassword"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
WEB.CONFIG TRANSFORMATION (DEV)
<connectionStrings>
<add name="OperationsDB"
connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=OPERATION;Persist Security Info=True;User ID=lab;Password=devPassword"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="AnalisysDB"
connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=lab;Password=devPassword"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
SQL ERROR (single-user limitation on dev environment)
Cannot open database "operation" requested by the login. The login failed. Login failed for user 'lab'.
And lastly, the way we connect it to the LINQ to SQL DBML
public partial class OperationDataContext
{
private const string OPERATION_CONN_STRING = "OperationsDB";
public OperationDataContext()
: base(System.Configuration.ConfigurationManager.ConnectionStrings[OPERATION_CONN_STRING].ConnectionString, mappingSource)
{
OnCreated();
}
}
public partial class AnalisysDataContext
{
private const string ANALISYS_CONN_STRING = "AnalisysDB";
public AnalisysDataContext()
: base(System.Configuration.ConfigurationManager.ConnectionStrings[ANALISYS_CONN_STRING].ConnectionString, mappingSource)
{
OnCreated();
}
}
This may be just a typo, but I did notice that the "Name" on Dev was "OperationsBD" and "AnaylsisBD", but the Transforms are set to match on name of "OperationsDB" and "AnaylsisDB". If this isn't a typo, it would never match, thus never replacing the connection strings.

Server specific Web.config replacement

I am currently using Visual Studio 2010. I want to set up a different connection string for my "Testing" Configuration. I have tried doing it by using config transformations, but it does not seem to work.
In my Web.config my connection string is the following:
<add name="MyDb" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DevelopmentDb;Integrated Security=SSPI; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
I found out that you can replace the connection string defined in Web.config by adding config transformation. I right clicked on Web.config and clicked on "Add config transformations". This created a Web.TEST.config file. I then added a connection string replacement, but it doesnt seem to work. It still uses the connection string defined in Web.config.
The file contents of Web.TEST.config is:
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an atrribute "name" that has a value of "MyDB".
-->
<connectionStrings>
<add name="MyDb"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDb;Integrated Security=SSPI; MultipleActiveResultSets=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
Any ideas to what I could be missing?
Transformations get applied when you deploy the project, not when you compile it.
If you are using IIS, you can deploy to the configured web application directory, which will cause the transform to apply.

Why am I getting a "No attribute 'name' exists for the Match Locator" error when publishing?

I am so confused on this. I want to use the SQL Server on my desktop when I am developing and using the live SQL Server when I publish my project. I am playing with the transformation stuff in Visual Studio 2010.
I get the "No attribute 'name' exists for the Match Locator" when I try to publish my project.
My Web.config file contains:
<connectionStrings>
<add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=db" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<sessionState mode="SQLServer" sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=eGov" timeout="20" allowCustomSqlDatabase="true" />
</system.web>
I am still testing it, so for now, my Web.Release.config file contains:
<connectionStrings>
<add name="EFDbContext"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=db"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<sessionState mode="SQLServer"
sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=app"
timeout="20" allowCustomSqlDatabase="true"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</system.web>
Anything I am seeing online is only confusing me more. Any help to get me up and running?
xdt:Locator="Match(name) mean that system will match nodes to replace using name tag. If you don't have name attribute then it fails. You must have some unique attribute to use this type of transformation.
Doh! The problem was in the sessionState section. It should be:
<system.web>
<sessionState mode="SQLServer"
sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=app"
timeout="20" allowCustomSqlDatabase="true"
xdt:Transform="SetAttributes" xdt:Locator="XPath(configuration/system.web/sessionState)" />
</system.web>
Using "name" in Match(name) is for a typical config setting like the following. The key in this case is "name".
<add name="errorAddress" email="me#google.com" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
If the key in your setting is something else, that is what you need to use:
<add token="UserToken" value="23jkl2klk2j3kja9d8f" xdt:Transform="SetAttributes" xdt:Locator="Match(token)"/>

Config Transform does not work?

I have this config in my Web.config file:
<connectionStrings>
<add name="LocalConnString" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NCU.Joberwocky.Db.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
This in my Web.Release.config:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="LocalConnString" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=192.168.10.3;Initial Catalog=AeorionJobs;User Id=aeorionjobs;Password=aejobs3695;MultipleActiveResultSets=True""
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
When I switch the build to release, it still uses the config from Web.config. Any idea what the problem is?
I am also struggling with configuration transforms. Actually I am trying to have a different configuration on my environment than the common one uploaded in code control.
I have been also trying the same thing as you are doing and I would say those transforms are only applied on the deployment packages that you can build.
Hope it helps.

VS2010 Build Deployment Package web.release.config transform error

I am getting the transformed web.config in the deployment package incorrectly transformed.
The result is as follows (notice how the connection string gets generated):
<connectionStrings>
<add name="xxxConnectionStringNamexxx"
connectionString="$(ReplacableToken_xxxConnectionStringNamexxx-Web.config Connection String_0)" providerName="System.Data.SqlClient" />
</connectionStrings>
Web.Config:
<connectionStrings>
<add name="xxxConnectionStringNamexxx"
connectionString="Data Source=.\sqlexpress2005;Initial Catalog=xxxx;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Web.Release.config:
<connectionStrings>
<add name="xxxConnectionStringNamexxx" connectionString="Data Source=.\sqlexpress2005;Initial Catalog=xxxx;Integrated Security=True" providerName="System.Data.SqlClient" xdt:Transform="SetAttributes(connectionString)" xdt:Locator="Match(name)"/>
</connectionStrings>
Any ideas?
Thanks
So benoit found an answer on the asp.net forums : ReplacableToken_ when using web.config transform? .
I deployed to a folder on my desktop and the web.config file was transformed correctly.

Resources