We have a different set of connection strings in our config file as follows.
<!-- TEST CONNECTION STRING -->
<!--
<add name="fooConnection" connectionString="Data Source=Test_server;Initial Catalog=foo_dbTEST;User ID=foo_user;Password=abc1234;"/>
<add name="barConnection" connectionString="Data Source=Test_server;Initial Catalog=bar_dbTEST;User ID=bar_user;Password=abc1234;"/>
<add name="chewConnection" connectionString="Data Source=Test_server;Initial Catalog=chew_dbTEST;User ID=chew_user;Password=abc1234;"/>
-->
<!-- LIVE CONNECTION STRING -->
<add name="fooConnection" connectionString="Data Source=Live_server;Initial Catalog=foo_dbTest;User ID=fooTest_user;Password=abc1234;"/>
<add name="barConnection" connectionString="Data Source=Live_server;Initial Catalog=bar_Testdb;User ID=barTest_user;Password=abc1234;"/>
<add name="chewConnection" connectionString="Data Source=Live_server;Initial Catalog=chew_Test;User ID=chewTest_user;Password=abc1234;"/>
<!-- Local DEV CONNECTION STRING -->
<!--
<add name="fooConnection" connectionString="Data Source=MyDoombaPC;Initial Catalog=fooDEVdbTest;User ID=foouser;Password=abc1234;"/>
<add name="barConnection" connectionString="Data Source=MyDoombaPC;Initial Catalog=barDEVdb;User ID=barTestuser;Password=abc1234;"/>
<add name="chewConnection" connectionString="Data Source=MyDoombaPC;Initial Catalog=chewDEVdb;User ID=chewuser;Password=abc1234;"/>
-->
When I need to change from, for example, live to test I move the XML comments from the Test section to the live section. Is there a more elegant way of doing this using Visual Studio 2010?
Just to be clear this is not a requirement of the application once it is in production, this is for developers to switch between different database connection string sets.
How do others approach this?
You could use the web.config transformation : web.config Transformation
This will allow you to have seperate config files for both debug and release and have different setting in each.
Related
Ok, I am using an external config file for my connection strings so each individual developer can have their own strings while developing. Normally, each dev has a different environment but we all publish to the same servers via web.release.config transforms.
However, when I publish from VS, it's not transforming from the web.release.config for the conn strings. I think it's because if you have the configSource attribute set to use an external config it ignores the transform.
Here's my web.config:
<connectionStrings configSource="userConn.config" />
And here's my userConn.config:
<?xml version="1.0"?>
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=XXXX;Initial Catalog=XXXX;user id=XXXX;password=XXXX;" />
<add name="ExtVariablesEntities"
providerName="System.Data.EntityClient" connectionString="metadata=res://*/Models.XXXX.csdl|res://*/Models.XXXX.ssdl|res://*/Models.ExtVariables.msl;provider=System.Data.SqlClient;provider connection string="data source=XXXX;initial catalog=XXXX;user id=XXXX;password=XXXX;MultipleActiveResultSets=True;App=EntityFramework;"" />
</connectionStrings>
After publishing, opening the actual web.config that made it to the server, it still has:
<connectionStrings configSource="userConn.config" />
Is there a workaround for this? I've had this setup before I just don't remember what the trick is.
Just an FYI that I was able to solve this by following this article: https://jshowers.com/simple-web-config-transforms-for-configuration-elements-that-use-configsource-and-external-files/
You end up with 3 custom config files - whatever you want to call them for dev, test and prod lets say. Then you will have 3 web config files dev, test and prod (these are your web config transform files) where you simply say:
<appSettings xdt:Transform="Replace" configSource="path.to.custom.config.file.depending.on.env">
I implemented ASP.NET Identity and it automatically created ASPNETDB.MDF and aspnetdb_log.ldf in my App_Data folder. I already have the AspNet tables (i.e., AspNetRoles, AspNetUsers, etc) in my SQL Express instance (which is where all my other tables are sitting). As far as I can see, my application is reading and writing membership and role data from the SQL Express database and not ASPNETDB.MDF.
I have set my connectionString in web.config to:
<add name="DefaultConnection" connectionString="Data Source=MyComputerName\SQLEXPRESS;Initial Catalog=MyDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
However, if I remove ASPNETDB.MDF from App_Data, I get the following error when I login:
Exception Details: System.Data.SqlClient.SqlException: One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.
Cannot open user default database. Login failed.
Login failed for user 'MyComputerName\MyUserName'.
Log file 'C:\Users\MyProjectName\App_Data\aspnetdb_log.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously
The error goes away once I add ASPNETDB.MDF back to App_Data.
I have searched all the code in my solution and it makes no reference to ASPNETDB. So why is it still trying to read from it?
I am developing ASP.NET web forms on .Net 4.5.
I was getting exactly the same problem. I discovered that VS annoyingly pulls in config settings from machine.config, which lives outside of the project, in my case in...
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
Identity 2.0 had used the following connection string inside machine.config...
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
to set up a connection for...
<membership>
<providers>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, ........" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>
.. (which was also set in machine.config). If you haven't been using Membership then it's fine to do as Lord nick suggests (except just the clear/ will do the job) and simply do the following in web.config...
<connectionStrings>
<clear/>
<add name="DefaultConnection" connectionString="whatever" providerName="System.Data.SqlClient" />
However, if you, like me, previously had Membership running (https://msdn.microsoft.com/en-us/library/6e9y4s5t(v=vs.100).aspx), you will need to comment out or delete the following sections from machine.config...
<!--
<membership>
<providers>
...
</providers>
</membership>
<profile>
<providers>
...
</providers>
</profile>
<roleManager>
<providers>
..
</providers>
</roleManager>
-->
... since all this stuff is no longer needed for AspNet Identity 2.
I also had to add the following into in my web.config:
<modules>
<remove name="RoleManager"/>
</modules>
... as per this answer: Default Role Provider could not be found on IIS 7 running .NET 4
I hope I've saved someone some time. This took me hours and hours to work out.
I had the same Problem where the ASPNETDB.MDF was automatically created, even if I use Asp.Net Identity as the main user management.
I solved it by removing the following line from web.config:
<roleManager enabled="true" />
This one tells ASP.NET to use the older ASP.NET Membership user management, which is not supported by ASP.NET Identity.
It seems you have something like in your web.config
<add name="DefaultConnectionForLocalDb" connectionString="Data Source=(LocalDb)\v11.0;..."; />
In your AccountModels.cs file, you have:
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnectionForLocalDb")
{
}
}
However it should be this way:
<add name="DefaultConnectionForSQLEXPRESS" connectionString="data source=.\SQLEXPRESS;...;" />
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnectionForSQLEXPRESS")
{
}
}
Then you can remove safely DefaultConnectionForLocalDb entry from your web.config and ASPNETDB.MDF from to App_Data.
I don't know if you've figured this out or not but one of the things you can try is: in web.config, connections section, add <Clear/> and then <Remove Name=LocalSqlServer/>
Apparently if you don't change/remove the LocalSqlServe will still try to connect to the aspnetdb.mdf.
You might also think about adding back in the LocalSqlServer and having it point to your SqlExpress or SqlServer.
I'm attempting to deploy my first website containing a database. The test local version has the following connection strings
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;IntegratedSecurity=SSPI;
AttachDBFilename=|DataDirectory|\aspnetdb.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
<add name="ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AHData.mdf;IntegratedSecurity=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I've setup the databases on the web hosting company server and they give me the following connections strings
Provider=sqloledb;Data Source=db479427514.db.1and1.com,1433;Initial Catalog=db479427514;User Id=dbo479427514;Password=****;
Provider=sqloledb;Data Source=db479427535.db.1and1.com,1433;Initial Catalog=db479427535;User Id=dbo479427535;Password=****;
When I replace the local test connection strings with the new server ones using
<connectionStrings>
<add name="ApplicationServices"
connectionString="Provider=sqloledb;Data Source=db479427535.db.1and1.com,1433;Initial Catalog=db479427535;User Id=dbo479427535;Password=**I've used real password!**;"
providerName="System.Data.SqlClient" />
<add name="ConnectionString"
connectionString="Provider=sqloledb;Data Source=db479427514.db.1and1.com,1433;Initial Catalog=db479427514;User Id=dbo479427514;Password=**I've used real password!**;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I get the following error
Keyword not supported: 'provider'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Keyword not supported: 'provider'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
[ArgumentException: Keyword not supported: 'provider'.]
[ArgumentException: An error occurred while attempting to initialize a System.Data.SqlClient.SqlConnection object. The value that was provided for the connection string may be wrong, or it may contain an invalid syntax.
Parameter name: connectionString]
Can anyone advise what is wrong here?
Try removing the 'provider' section of the connection string:
<add name="ApplicationServices" connectionString="Provider=sqloledb;Data Source=db479427535.db.1and1.com,1433;Initial Catalog=db479427535; User Id=dbo479427535;Password=**I've used real password!**;" providerName="System.Data.SqlClient" />
becomes
<add name="ApplicationServices" connectionString="Data Source=db479427535.db.1and1.com,1433;Initial Catalog=db479427535;User Id=dbo479427535;Password=**I've used real password!**;" providerName="System.Data.SqlClient" />
Because the provider name is specified in it's own name-value pair (providerName=) I doubt it would be needed in the connection string. Give it a crack and report back.
Correct your Connecting sting to:
<connectionStrings>
<add name="ApplicationServices" connectionString="
Data Source=db479427535.db.1and1.com,1433;Initial Catalog=db479427535;
User Id=dbo479427535;Password=**I've used real password!**;"
providerName="System.Data.OleDb" />
<add name="ConnectionString" connectionString="
Data Source=db479427514.db.1and1.com,1433;Initial Catalog=db479427514;
User Id=dbo479427514;Password=**I've used real password!**;"
providerName="System.Data.OleDb" />
</connectionStrings>
This is because we dont have provider object in connection string, so remove that and specify the provider name under "providerName" section of connection string to: System.Data.OleDb
Can someone point me to what am I doing wrong here?
I'm trying to set up an ASP.NET web app project to compile with two versions of web.config file for Release and Debug builds. So for simplicity sake, here's my web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="WhyMicrosoftSucksSoMuch" connectionString="" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
And then I do the following in web.debug.config:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="WhyMicrosoftSucksSoMuch"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='C:\FilePath\Database1.mdf';User Instance=true"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(connectionString)" />
</connectionStrings>
</configuration>
If I publish it under Debug configuration the resulting web.config looks good, but when I try to run my project from VS2010 also under Debug configuration I get an error when my logic attempts to access database:
The ConnectionString property has not been initialized.
So what's the trick here?
PS. And please don't point me to this document. I tried reading it several times but I get a headache from so much superfluous information. I guess MS doesn't know what brief is.
When running it under debug mode it doesn't apply any transformations.
It only applies them during publishing. You can put your debug connection string in the main web.config, and add your production connection string to the web.release.config
also, you probably will want to use
xdt:Transform="Replace"
I am using Enterprise library for my data access.
When I am running the application, at the CreateDatabase() statement I am getting this exception:
Microsoft.Practices.ObjectBuilder2.BuildFailedException
was unhandled by user code
Message="The current build operation
(build key Build
Key[Microsoft.Practices.EnterpriseLibrary.Data.Database,
null]) failed:
The value can not be null or an empty string.
(Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy,
index 2)"
Source="Microsoft.Practices.ObjectBuilder2"
Now, I googled a bit and I found that I have to place
<dataConfiguration defaultDatabase="LocalSqlServer"/>
but I don't know where. Is it the right solution?
Also, at the time of installing enterprise library I didn't see any connection string statement? So, I wonder how it will take the connection string from web.config file.
In the connection string section of my web.config file I have:
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=MSTR;Initial Catalog=USERDb;Integrated Security=true;" providerName="System.Data.SqlClient"/>
Yes you need to add the dataConfiguration section to the web.config.
First you need to add dataConfiguration to the list of ConfigurationSections in your web.config:
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</configSections>
Then you need to add your connection strings to the web.config (you've already done this):
<connectionStrings>
<add name="LocalSqlServer" connectionString="Data Source=MSTR;Initial Catalog=USERDb;Integrated Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Then you need to add the actual dataConfiguration section to the web.config:
<dataConfiguration defaultDatabase="LocalSqlServer"/>
You can also use the Enterprise Library Configuration Tool to do this for you as well.