ASP.NET MVC : web.config connection string for multiple developers - asp.net-mvc

I have been looking and reading, and I could not find this solution yet.
I have a small ASP.NET MVC project I am working with two other developers, we have the same settings, just the computer name is different.
So, our connection strings section in web.config looks something like this:
<!-- Hugo
<add name="DefaultConnection" connectionString="data source=DEKSTOP123\DATABASE;..."/>
<add name="OtherConnection" connectionString="metadata=res:...;data source=DESKTOP123\DATABASE;..."/>
-->
<!-- Paco -->
<add name="DefaultConnection" connectionString="data source=DEKSTOP456\DATABASE;..."/>
<add name="OtherConnection" connectionString="metadata=res:...;data source=DESKTOP456\DATABASE;..."/>
<!-- Luis
<add name="DefaultConnection" connectionString="data source=DEKSTOP789\DATABASE;..."/>
<add name="OtherConnection" connectionString="metadata=res:...;data source=DESKTOP789\DATABASE;..."/>
-->
We just comment and uncomment whatever connections is ours, but it's messy. Is there a better solution for this?

Related

Is there a way to check debug=true in startup.auth in ASP.NET MVC

I see that we can use HttpContext.Current.IsDebuggingEnabled in controllers to check if debug=true is enabled in web.config. How can I check the same in startup.auth? I need to set different app Ids for my local and production environments for fb authentication using this condition. Please suggest if there is a better way to do this.
I would recommend creating a web.config entry for whatever dynamic data you need to set and then use web.config transforms to change the data depending on environment. This is very commonly used with connection strings to set different source databases depending on what environment youre in (local vs test vs prod).
See the example below:
local Web.config:
<connectionStrings>
<add name="myConnection" connectionString="Data Source=localhost;Initial Catalog=myDatabase;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Web.Debug.Config:
<connectionStrings>
<add xdt:Transform="SetAttributes" xdt:Locator="Match(name)" name="myConnection" connectionString="Data Source=myTestSqlServer;Initial Catalog=myTestDb;User Id=myTestUserId;Password=myTestPassword;" providerName="System.Data.SqlClient" />
</connectionStrings>
Web.Release.Config:
<connectionStrings>
<add xdt:Transform="SetAttributes" xdt:Locator="Match(name)" name="myConnection" connectionString="Data Source=myProdSqlServer;Initial Catalog=myProdDb;User Id=myProdUserName;Password=myProdPassword;" providerName="System.Data.SqlClient" />
</connectionStrings>

ASP.NET MVC 5 change database name

Hy!
I'm new to ASP.NET MVC 5. I try to change the default database name. But I don't know how. My goal is to have only one database for the whole application.
My actual working Web.Config:
<connectionStrings>
<add name="DefaultConnection" connectionString="DataSource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-SqlTest-20131122100021.mdf;Initial Catalog=aspnet-SqlTest-20131122100021;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
This Web.Config file should look like this and should work ^^ (actual I get a error that the database could not be created and the physical name may be incorrect)
<connectionStrings>
<add name="DefaultConnection" connectionString="DataSource=(LocalDb)\v11.0;AttachDbFilename=Example.mdf;Initial Catalog=Example;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Thanks for help :)
You need to add |DataDirectory|\ before Example.mdf, like below:
<connectionStrings>
<add name="DefaultConnection"
connectionString="DataSource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Example.mdf;Initial Catalog=Example;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

where is aspnetdb.mdf in mvc3 default sample program?

In the default sample appication in mvc3, Where do the details we fill in registration form gets stored?
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
I did not find any aspnetdb.mdf file in app_data folder.
You need to click the 'show all files' option as it is hidden IIRC.

I can't seem to reference a class inside App_Code for model to get web config strings

I'm working on a project where we're trying to encapsulate all references of two databases on two different servers. We have this in a "utilities" file inside App_Code:
public static string myFirstDBName = System.Configuration.ConfigurationManager.AppSettings["firstDbName"];
public static string mySecondDBName = System.Configuration.ConfigurationManager.AppSettings["secondDbName"];
Which we can reference in any Controller no problem.
However, when I try to reference this in a model it doesn't see it - I tried a using statement and it didn't work. I'm hoping to pull this out of the model and put it in the same utilities file:
private readonly string _myFirstDBName = WebConfigurationManager.AppSettings["firstDBName"];
private readonly string _mySecondDBName = WebConfigurationManager.AppSettings["secondDBName"];
Can someone tell me if I'm missing anything? Or should we be putting this utilities file somewhere else? Thanks in advance!
Edit: Added my web.config values to correspond with this question:
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="firstDBName" value="db1" />
<add key="secondDBName" value="db2" />
</appSettings>
try web.Config
<configuration>
<connectionStrings>
<add name="DBName" connectionString="Data Source=(local);Initial Catalog=DBName;User ID=uid;Password=pwrd;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
you can also add app settings in web.Config
<appSettings>
<add key="KEY" value="VALUE" />
</appSettings>
update in regards to comment...
take this example
Model m = new Model();
m.prop = System.Configuration.ConfigurationManager.AppSettings["firstDBName"];
m.prop2 = System.Configuration.ConfigurationManager.AppSettings["secondDBName"];
return View("VIEWNAME", m);

SimpleMembership API connectionStringName error

I am trying to use SimpleMembership in my MVC 3 application. However I get the following error:
System.ArgumentException was unhandled
by user code. Unable to find the
requested .Net Framework Data
Provider. It may not be installed.
I'm using EF and the framework is obviously there since my app works perfect without the SimpleMembership API
Here is the Set Up in the Web config file:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="SeniorProjectModelContainer" connectionString="metadata=res://*/Models.SeniorProjectModel.csdl|res://*/Models.SeniorProjectModel.ssdl|res://*/Models.SeniorProjectModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=GOGOTOPPY;Initial Catalog=SPMT;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
And heres how it looks in the in the Membership file in the App_Start folder:
WebSecurity.InitializeDatabaseConnection(connectionStringName: "SeniorProjectModelContainer", userTableName: "Users", userIdColumn: "UserId", userNameColumn: "UserName", autoCreateTables: true);
Any help would be greatly appreciated!
Update:
The quick work around was to take the metadata part out of the connection string and change the provider name to the SqlClient so it looks similar to this:
<connectionStrings>
<add name="Membership" connectionString="Data Source=serverName;Database=datebaseName;User ID=idName;Password=password;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Is there any downside of the connection string looking like this?
Not sure how you installed simplemembership but try doing it the nuget way for example,
http://www.nuget.org/List/Packages/SimpleMembership.Mvc3

Resources