Tom Squires
Tom Squires

Reputation: 9286

MVC - Use remote SQL database

How do you change an MVC3 web app to use a remote server for the data context? I tried changing the connection string the in web config (and restarted everything) but it still uses the local SQL database.

Edit: my connection string is below

<add name="ApplicationServices" connectionString="Data Source=zzz.database.windows.net;Initial Catalog=SMS;Persist Security Info=True;User ID=xyz;Password=abc" providerName="System.Data.SqlClient" />

Edit 2: I tried commenting out everything inside the <connectionStrings> tag. The site still works. I'm a bit confused, is the connection set somewhere else?

Upvotes: 1

Views: 2612

Answers (2)

Jesse van Assen
Jesse van Assen

Reputation: 2290

If you are using Entity Framework code first (which comes with asp.net mvc 3), the name of the database class corresponds with the name of the connection string. For example if you have a class like this:

public class DataContext : System.Data.Entity.DbContext
{
    [...]
}

The name of your connection string should also be DataContext. More info here at step 4.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You change the connection string in your web.config. For example if you are using a SQL server your connection string could look like this:

<add name="ApplicationServices"
     connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
     providerName="System.Data.SqlClient" />

where Data Source points to the name of the remote SQL server, Initial Catalog the name of the database to connect to and User Id and Password seem pretty self explanatory.

Now obviously in your code you will use the ApplicationServices connection string somewhere, so depending on what data access you are using or whether you are using an ORM there might be different places to look for. So if despite changing the connection string in your web.config your code continues to use a local database, well, it's obvious that this is hardcoded somewhere in your code.

Upvotes: 0

Related Questions