1

Can somebody tell me how I can setup my MVC3 applciation so that when it first creates a database that it does so in a local (or remote) instance of SQL Server 2008 instead of using SQL Server Express?

2 Answers 2

2

So if you're using EF, then you will use a class to connect such as

public class EFDbContext : DbContext
    {
        public DbSet<Product> Products
        {
            get;
            set;
        }
    }

Now, you only need a connection string in your project's WebConfig file (not the webConfig in the Views folder). Add a connectionStrings section under the configuration node like this. NOTE: The class and the connection string must share an identical name - in this case, "EFDbContext".

<configuration>
  <connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=SERVERNAME\;Initial Catalog=DATABASENAME;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD" 
    providerName="System.Data.SqlClient"/>
  </connectionStrings>

"SERVERNAME\" will connect you to the default installation. If you're looking for the default installation on your local machine, just enter your computer name and you're golden. If you prefer to use window authentication rather than SQL Server authentication, just substitute "Integrated Security=true" for the UserID/Pwd portion of the connection string. HTH

Sign up to request clarification or add additional context in comments.

2 Comments

I changed the <connectionStrings/> in the webconfig (not in the views) and added a new connection string and commented out the old one. It didn't work in that it still connected to the original one. In fact if I comment out the entire connection string section the application still runs and connects to the old database and loads data. I have tried cleaning the project and deleting temp files but it has the old database cached somewhere somehow.
Use Ctrl-F to search for the DB and check the Server Explorer for any objects/connections.
1

You can choose connection string using constructor as following way :

Public class EFDbContext : DbContext
{
   public EFDbContext() : base("dbconninfo"){}
}

<configuration>
  <connectionStrings>
    <add name="dbconninfo" connectionString="Data Source=SERVERNAME\;Initial Catalog=DATABASENAME;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD" 
    providerName="System.Data.SqlClient"/>
  </connectionStrings>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.