Difference between my local and production web.config - c#

I get this error message
The connection string 'MyConnection' in the application's
configuration file does not contain the required providerName
attribute."
I read this question The connection string 'MyConnection' in the application's configuration file does not contain the required providerName attribute." about this same issue.
In my case the providerName="System.Data.SqlClient" is correctly in my web.config file in local but disappear when I do a publish on my production server (build in release). I don't have the problem on my local server.
EDIT
Here is my release config file
<?xml version="1.0" encoding="utf-8"?>
<!-- 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 attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
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>

Use this for your release config file:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" providerName="System.Data.SqlClient" />
</connectionStrings>
ConnectionString for your local is being change by release config file. So if you declare providerName in your local config, that will be no use if you publish your release files. you need to declare the providerName in your release config file also.

Related

Connection strings in Entity Framework

I am referencing 2 databases in ASP.NET using Entity Framework.
In my web.config file, I can see the connection strings for the 2 databases:
<connectionStrings>
<add name="RContext"
connectionString="metadata=res://*/Models.RModel.csdl|res://*/Models.RModel.ssdl|res://*/Models.RModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="CEntities"
connectionString="metadata=res://*/Models.CModel.csdl|res://*/Models.CModel.ssdl|res://*/Models.CModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Can I somehow implement alternate connection strings where the datasource refers to the prod server for the release?
This is typically handled with web.config transforms.
In your project you would have:
web.config
web.Release.config
For example in your web.Release.config transform you would have something like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="RContext"
connectionString="RContext-Prod-Connection-String"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="CEntities"
connectionString="CEntities-Prod-Connection-String"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
You'll notice the xdt:Transform="SetAttributes" xdt:Locator="Match(name)" bit, which says, in the main web.config find the connectionString by name and replace its attributes with the ones defined here.
This will automatically happen when you publish the application.

web.debug.config does not work

I have a webb aplication where I want to take advantage of the web.debug.config file so that when debugging I use a test database. This does not work for me. I have this in my web.debig.config..
<connectionStrings>
<add name="connStr"
connectionString="Data Source=LocalSqlserverName;Initial Catalog=testdb;Persist Security Info=True;User ID=Myusername;Password=mypassword"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
And in my web.config file this connectionstring point to another database server. But when I debug I can see that the connectionstring in web.config file is used instead of the one in web.debug.config. What am I doing wrong here?
As #esiprogrammer says it normally only transforms on Publish from Visual Studio.
However you can transform Web.config on build. Add this to your *.csproj file:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild">
<TransformXml
Source="Web.Base.config"
Transform="Web.$(Configuration).config"
Destination="Web.config" />
</Target>
Keep the origin configuration in Web.Base.config. It's enough to enable transformation and it works for any XML config file.
Source:
https://stackoverflow.com/a/35561167/3850405

Edit Web.Config after publishing in asp.net IIS server

I have a developed a application in c#,asp.net web application(using only inside our company) and finally am deploying the application in server PC (different host and port )allocated for this purpose.
When i developing or maintaining the application in my PC, having different host and port, every time when i need to publish im changing the connection string in web.config and and copying app folder in server PCs, interpub->wwwroot.
Is there any option to avoid each time changes? i.e, for publishing i will use seperate web.config and for developing work i use seperate web.config.
Server PC- web.config, connection string:
<connectionStrings>
<add name="MySqlConnectionString" connectionString="server=192.168.3.5;user id=root;password=sim;database=simpayroll;allowuservariables=True;port=3306"
providerName="MySql.Data.MySqlClient" />
<add name="simpayrollConnectionString" connectionString="server=192.168.3.5;user id=root;port=3306;password=sim;database=simpayroll;persistsecurityinfo=True;allowuservariables=True"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
My local Web.config
<connectionStrings>
<add name="MySqlConnectionString" connectionString="server=localhost;user id=root;password=root;database=simpayroll;allowuservariables=True;port=3306"
providerName="MySql.Data.MySqlClient" />
<add name="simpayrollConnectionString" connectionString="server=localhost;user id=root;port=3306;password=root;database=simpayroll;persistsecurityinfo=True;allowuservariables=True"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
You can use web.config transformations to over come with your problem
In your Web.Release.Config file,
<connectionStrings xdt:transform="Replace">
<add name="MySqlConnectionString" connectionString="your connection string"
providerName="MySql.Data.MySqlClient" />
<add name="simpayrollConnectionString" connectionString="your connection string"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>

Restless connection to the Entity Framework

I have an ASP.net web application hosted in IIS. This application use SQL Server.
I removed connection string in web.config. But when I start the application, it is still connected to it.
The connection string was nowhere explicitly stated.
how does it work?
By default the machine.config located in C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config have the following connection string configured.
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
By leaving the <connectionStrings> section of your web.config empty you are not removing the default connectionstring. To ensure that you have no configured connection string you should use a <clear /> element in your web.config file.
<connectionStrings>
<clear />
</connectionStrings>

Encryption web.config file "aspnet_regiis.exe" not working

I work with asp.net fromwork 4.0 web site. and I want to encrypt web.config file connectionString. I used aspnet_regiis -pef "connectionString" "file Location"
but it's not working. Rather I'm getting only the error message as shown below
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime/>
</system.web>
<connectionStrings>
<add name="MS_EAS_LocationConnectionString" connectionString="Data Source=help\sqlexpress;Initial Catalog=MS.EAS.LOCATION;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

Categories