Is there a way to make EF 5 code first migrations use a sql server database in ASP.NET MVC 4 for everything?

≯℡__Kan透↙ 提交于 2019-12-01 00:24:28

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.

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")
    {

    }

    // ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!