Skip to content Skip to sidebar Skip to footer

Is There A Way To Make Ef 5 Code First Migrations Use A Sql Server Database In Asp.net Mvc 4 For Everything?

I would like for it to use SQL Server by default for everything when I start a new ASP.NET MVC 4 Web Application project. By default when you run this project it creates a LocalDb

Solution 1:

Open your InitializeSimpleMembershipAttribute.cs file, this is where the WebSecurity database initialization is. You need to modify it with the correct connectionStringName. Example:

Change

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

to

WebSecurity.InitializeDatabaseConnection("MyRealDBConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

IF you want to copy the structure of the web security tables to use with Code First, there is an article .... which I cannot find at the moment ... give me a few.

Well I could not find it - but honestly - it was a pain. The easiest way, since you have the DB generated already, might be to use an Code First reverse engineer tool like Entity Framework Power Tools. This will do most of the work for you. Then just add the classes to your DbContext, create a migration, and update your real database.

Also - you may need to make more modifications than this - depending on your Context name and such.

Solution 2:

If your goal is to only create those tables in your existing db, you should perform 2 steps.

First, change the connection string of db initialization as what @Jack said.

Second, in your DbContext, change the constructor like this:

public class MyDbContext : DbContext
{
    publicMytDbContext()
        : base("MyRealDBConnection")
    {

    }

    // ...
}

Post a Comment for "Is There A Way To Make Ef 5 Code First Migrations Use A Sql Server Database In Asp.net Mvc 4 For Everything?"