serlingpa
serlingpa

Reputation: 12680

How to specify a database when using InitializeDatabaseConnection()

I am working with the default Internet template when using MVC 4.0. How can I specify where the initial database is created? I have installed SQL Server Express 2012 and want the database created there instead of in the Visual Studio built-in SQL Server.

I did try using the default and then copying the database from VS to SQL Express 2012 but I don't seem to be able to back up the VS database.

Anyone have any idea?

Thanks, Mark

Upvotes: 0

Views: 84

Answers (2)

MarkG
MarkG

Reputation: 1869

You need to add a connection string in the config which has a name matching the name of your context class, for example:

<connectionStrings>
    <add name="FooBarContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=FooBar;Integrated Security=SSPI;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>

In the above, my context class is named FooBarContext which matches the connection string name. The context class looks something like this:

public class FooBarContext : DbContext
{
    // etc.
}

Upvotes: 1

hVostt
hVostt

Reputation: 1681

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "Id", "Login", autoCreateTables: true);

where DefaultConnection — connection name in your web.config (in <connectionStrings>)

Upvotes: 0

Related Questions