Reputation: 85
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?
Upvotes: 1
Views: 1360
Reputation: 4108
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>
Upvotes: 1
Reputation: 688
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
Upvotes: 2