Forms authentication and code first migrations RRS feed

  • Question

  • I previously asked [here] why the MVC template code uses convoluted code (in the InitializeSimpleMembershipAttribute [ISMA] filter class) to initialize the membership database.  The answer was that this works around the following bug:

    My problem is that if I let the ISMA code create the database then enable-migrations does not create the initial migration to create the database.  If I manually create the initial migration (via add-migration) then it complains when I try to apply it (via update-database) because the table(s) already exist.  And if I don't apply it then it complains that there is a pending migration.

    If I replace the ISMA code that creates the database if it doesn't exist with context.Database.CreateIfNotExists() then the database is created with the migration magic baked into it and everything works like it should.

    So, my question is: Now that the above "work item" claims that the problem is fixed, is there still a reason why I should not replace the ISMA code that creates the database if it doesn't exist with context.Database.CreateIfNotExists()?

    Friday, November 16, 2012 9:12 PM

All replies

  • Hi Bob,

    Welcome to the MSDN forum.

    I am trying to involve another expert in your thread. Please wait for the response. Sorry for any inconvenience.

    Best Regards,

    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 19, 2012 8:26 AM
  • Hi Bob,

    The issue is fixed in Entity Framework 6. I believe MVC is dependent on Entity Framework 5, however.


    Cathy Miller

    Wednesday, November 21, 2012 6:59 PM
  • Thanks Cathy.  After much experimentation, I settled on the following solution to my problem.  Note that I'm wiring up my Users table to the SimpleMembership magic.  The Users table has a bunch of subordinate tables in my schema.  If there is a potential problem with this solution, please let me know.

    1. I removed the ISMA filter altogether.

    2. I added a call to MyModel.Initialize() in the Global.asax Application_Start routine.  MyModel.Initialize looks like this:

    public class MyModel { static bool _initDone = false; public static void Initialize() { if (!_initDone) { _initDone = true; // Automatically apply any pending migrations Database.SetInitializer(
    new MigrateDatabaseToLatestVersion<MyDbContext, Configuration>());

    // Touch the database to create and initialize it if necessary using (var db = new MyDbContext()) { db.Users.Count(); } // Initialize the SimpleMembership provider WebSecurity.InitializeDatabaseConnection( "MyDbConnection", "Users", "Id", "UserName", autoCreateTables: true); } } }

    This seems to work well for me.  If I delete the database and the migrations folder and run the project on my local PC then the database is recreated, and I can use Enable-Migrations to create the initial migration.  Thereafter, if I update the code-first database schema and execute add-migration then, the next time I run the project, magic happens and the database is upgraded to the current schema.

    • Edited by Thursday, November 22, 2012 1:00 AM
    Thursday, November 22, 2012 12:52 AM