How to connect to different databases for a .NET MVC web application written in C#?
Here the stucture of the database table remains the same. All that changes is the database name. So how to I manually connect to a database or use modify the connection string?
The trick is to use the same code. Depending on the URL I want the data to be displayed from different database. So the Views and Controller (pretty much) has single code w.r.t. the Model.
-Datte
Here's a very simple way to do what I think you're asking. In your web.config file, you could define your two connection strings:
<connectionStrings>
<add name="DevelopmentDB" providerName="System.Data.SqlClient"
connectionString="Data Source=sql-dev.example.com;Initial Catalog=MyDB;User Id=MyUser;Password=MyPassword" />
<add name="ProductionDB" providerName="System.Data.SqlClient"
connectionString="Data Source=sql-prod.example.com;Initial Catalog=MyDB;User Id=MyUser;Password=MyPassword" />
</connectionStrings>
Then, in your (base) controller, you could create a method that returns the appropriate connection string based upon the request, such as:
internal string ConnectionString
{
get
{
return getConnectionStringByServerName(this.HttpContext.Request.ServerVariables["SERVER_NAME"]);
}
}
internal string getConnectionStringByServerName(string serverName)
{
if (serverName.Equals("localhost"))
{
return WebConfigurationManager.ConnectionStrings["DevelopmentDB"].ConnectionString;
}
else
{
return WebConfigurationManager.ConnectionStrings["ProductionDB"].ConnectionString;
}
}
You could of course change the selection criteria for whatever makes the most sense.
Good luck!
Related
I am trying to move my 2 database connection strings to environment variables for security reasons. Everything works fine when I include the 2 connection strings on web.config like so:
<connectionStrings>
<clear />
<add name="DefaultConnection" connectionString="Data Source=xxxxxx" providerName="System.Data.SqlClient" />
<add name="RDSContext" connectionString="Data Source=xxxxxx" providerName="System.Data.SqlClient" />
</connectionStrings>
I then removed the 2 connection strings from web.config and created 2 environment variables as follows:
setx CUSTOMCONNSTR_DefaultConnection "Data Source=xxxxx"
setx CUSTOMCONNSTR_RDSContext "Data Source=xxxxx"
Although I now get the following error when I startup IIS and visit the web app
Server Error in '/' Application.
Cannot attach the file 'C:\Users\xxx\xxx\App_Data\BookingSystem.Models.RDSContext.mdf' as database 'BookingSystem.Models.RDSContext'.
Can anyone tell me what I am doing incorrect?
Simply naming your environment variable in a certain way doesn't mean that MVC will pick them up. You may be assuming a little more magic is happening than there actually is.
When you do ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString you're only retrieving the connection string from your web.config file, or machine.config if you happened to have defined it there. There's nothing there to tell your app to grab it from anywhere else.
Instead, you might consider creating a class to represent your configuration:
public class MyApplicationDatabaseConfiguration
{
public string ConnectionString { get; set; }
}
Any class that needs to obtain this connection string can do so by depending on an instance of that MyApplicationDatabaseConfiguration.
public class MyDatabaseRepository
{
readonly MyApplicationDatabaseConfiguration _dbConfig;
public MyDatabaseRepository(MyApplicationDatabaseConfiguration dbConfig)
{
_dbConfig = dbConfig;
}
public void DoSomethingWithTheDatabase()
{
using(var connection = new SqlConnection(_dbConfig.Connectionstring))
{
//now you can use the connection
}
}
}
Then you can load your config however you like. Ex:
MyApplicationDatabaseConfiguration dbConfig = new MyApplicationDatabaseConfiguration();
dbConfig.ConnectionString = Environment.GetEnvironmentVariable("MyApplication_MyConnectionString");
Now the MyDatabaseRepository doesn't care how you load the database config, it simply says "I need the database config". This is extremely powerful, it allows you to change out the configuration by simply changing a line of code. Need to change back to using web.config? It's as simple as dbConfig.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString;
Microsoft has taken this one step further in Microsoft.Extensions.Configuration library, which is meant for .NET Core but targeted to .NET Standard, so you can use it in your .NET Framework library if you'd like.
SHORT VERSION: Is it possible and how to define a variable in a web.config file that I can use within itself.
I am new to a company and also a beginner developer in C# MVC Web applications. I have been tasked to maintain and test a web application that has multiple connections strings in a single web.config file. These connection strings, however, have the same values in it like:
<connectionStrings>
<add name="DefaultConnection" connectionString="provider=System.Data.SqlClient;provider connection string="data source=SUPERCOMPUTER\SQLEXPRESS2012;initial catalog=HumanResourceDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="DefaultConnection3" connectionString="Data Source=SUPERCOMPUTER\SQLEXPRESS2012;Initial Catalog=HumanResourceDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
As you can see, the difference with these two is just the Name of the connection. Since Im a beginner I don't exactly know why this application has to use 2 same connection strings with different names. I was told that one is being used by Database First entity framework and the other is being used by ASP.NET Identity Code First and I do not know right now what those are.
The problem: I am CONSTANTLY changing connection strings value to test with different machines/environment/database etc and the need to modify 2 connection strings is really slowing me down.
The question: Is it possible to use variables that I can just declare at the top of the web config file and use it in the connection strings so that I can just modify the variable values. Something like this:
var Server = SUPERCOMPUTER\SQLEXPRESS2012
var IntegratedSecurity = true
var database = HumanResourceDB
var user = databaseusername
var password = databasepassword
<connectionStrings>
<add name="DefaultConnection" connectionString="... data source=**Server**;initial catalog=**database**;integrated security=**IntegratedSecurity **;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="DefaultConnection3" connectionString="Data Source=**Server**;Initial Catalog=**database**;Integrated Security=**IntegratedSecurity**" providerName="System.Data.SqlClient" />
</connectionStrings>
Additional Question: Is it really impossible to use one connection string if the mvc application relies on both Database First Entity Framework and Code First .NET Identity?
All EF project have a dbContext File..so you can use multiple EF projects(Code First, DB first) in just one solution.
Yes its clearly possible ...in code first your db context can get the name of each ConnectionString you want!.. like this:
public class AppContext : DbContext
{
public AppContext() : base("ConString Name")
{
}
}
so you can change the connection string name to another one..like DefaultConnectionString1 to DefaultConnectionString12 or 1000 ...
and you can have multiple db context to work on multiple databases to...
in Code first from database is like that to ...so in each part of project you can make instanse of which dbcontext you like. and do CRUD operation on that..
So I have Solution with two projects. The first projects should act as a Data Access Layer so there in the app.config file I have this:
<connectionStrings>
<add name="BloggingContextCF"
connectionString="provider=System.Data.SqlClient;
provider connection string="
data source=*****\SQLEXPRESS;
initial catalog=CodeFirst.Blogging;
integrated security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
The second Project is a ASP.NET Web Forms project, where I removed the default connection string pointing to the (local) instance leaveing only one connection string, the one from the first Data Access Layer project. Which actually works and the database is created on my SQL 2008 R2 server, but instead of the desired (and expected) database name - CodeFirst.Blogging the name is DataAccessLayer.BloggingContextCF where DataAccessLayer is the name of the project and BloggingContextCF is the class extending DbContext.
What I need to change in my connection string so I can get the desired name?
Try
public BloggingContextCF ():base("CodeFirst.Blogging") {}
make the connections string like
Point it to sql client
providerName="System.Data.EntityClient"
to
providerName="System.Data.SqlClient"
For full connection string do it like
<add name="BloggingContextCF" connectionString="Server=YOUR_SERVER;Database=DATABASE_NAME;User ID=USER_ID;Password=PASSWORD;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />
For more connectionstring example follow the link
and don't forget to modify your dbcontext
public class YourContext : DbContext
{
public UsersContext()
: base("BloggingContextCF"){ }
}
I have a local ASP.NET MVC 3 Windows Azure Project where I use a local instance of MSSQL Express for my emulator.
In my web.config I have the following connection string:
<add name="ExampleDb" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ExampleDb;User Instance=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
For debugging purposes I have the following in my WebRole.cs file:
public override bool OnStart()
{
ExampleDb context = new ExampleDb();
context.ExampleItemEntries.Add(new ExampleItem() { ExampleItemId = 1, Att1 = "1", Att2 = "2" });
context.SaveChanges();
return base.OnStart();
}
When I perform this action I can actually see the content in my code-first generated database (using Entity Framework). Database: ExampleDb, Table: ExampleItem.
However, doing the exact same thing in ExampleController does NOT update the database. Instead this data goes somewhere else (I have no idea where). When I visit my controller via the browser I can upload data and look at it but it is not stored in my ExampleDb database, only data sent from WebRole.cs is in the database.
Any ideas what's causing this behaviour?
ExampleDb.cs looks like this:
public class ExampleDb : DbContext
{
public ExampleDb() : base("ExampleDb") { }
public DbSet<ExampleItem> ExampleItemEntries { get; set; }
}
Your connection string contains 'User Instance=True'. This means that SQLEXPRESS uses a user instance to store your data. This is a completely different instance of SQL Server that contains a new set of databases.
I assume the code in WebRole.cs is run in a different user account than the code in ExampleController. When the ExampleController is run under a user account that is not an administrator, a user instance is used...
So when you remove 'User Instance=True' from your connectionstring and assign the proper access rights to your SQLEXPRESS database instance, that would solve the problem.
In CodeFirst Model you should define the connection string in the web.config first as below:
<connectionStrings>
<add name="ExampleDbConnection"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
database=ExampleDb;
AttachDBFilename=|YouDataDirectory|yourdb.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
<connectionStrings/>
The DbContext class constructor accepts a name-value pair specifiying connection-string name in web.config. So you can reference this connection string in your context:
class ExampleDb : DbContext
{
public ExampleDb() : base("name=ExampleDbConnection") { }
...
}
This code does work fine in a ASP.NET MVC Web Role so you can try as above.
An application of mine uses LINQ-to-SQL extensively, and it is now a requirement of the application to be able to switch which database it is looking at at runtime - so essentially I would like to be able to choose the connection string of my data context when I declare it.
Is there an easy way of doing this?
Just call :
DataContext context = new DataContext ("cxstring");
You could use an App.config to store your Connection Strings then use them to populate a drop down box or something. Then use the selected Connection string in the constructor of your LINQ2SQL data context.
App Config:
<configuration>
<connectionStrings>
<add key="ConString1" connectionString="ConnectionStringGoesHere"/>
<add key="ConString2" connectionString="ConnectionStringGoesHere"/>
</connectionStrings>
Use the ConfigurationManager class to access your connection strings.
string conString = ConfigurationManager.ConnectionStrings["ConString1"].ConnectionString;
You can also enumerate over them or set them as datasource to populate a drop down box.
Then simply pass the selected string in as the first parameter in your LINQ2SQL datacontext constructor.
MyModelDataContext context = new MyModelDataContext(selectedConString);
If you mean by switching which database your application is looking at ,Test database and production database , simply you can make two connection string in your web.config file with the same key but has different connection string and comment one of them according to the desired database
<add name="MyConnectioString" connectionString="Data Source=myServer;Initial Catalog=ProductionDB;" providerName="System.Data.SqlClient" />
<!--<add name="MyConnectioString" connectionString="Data Source=myServer;Initial Catalog=TestDB;" providerName="System.Data.SqlClient" />-->
by comment and uncomment you can switch between the 2 databases in run time.
to choose the connection string of your context provide it with its constructor
DataContext Productioncontext = new DataContext ("MyConnectioString");