none
ObjectContext.CreateDatabase vs. context.Database.CreateIfNotExists RRS feed

  • Question

  • Hi all,

    In VS 2012, the MVC 4 template creates code in InitializeSimpleMembershipAttributes.cs that looks like this to create the membership database if it does not already exist:

    using (var context = new MyDbContext()) {
      if (!context.Database.Exists()) {
        // Create the SimpleMembership database without Entity Framework migration schema
        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
      }

    I have several questions about this code:

      • Why jump through all of those hoops when they could have just replaced the entire if block with a call to context.Database.CreateIfNotExists()?
      • Even if there is a good reason to explicitly check to see if the database exists, why go to all that work to call ObjectContext.CreateDatabase rather than just calling context.Database.Create()?
      • What is the "Entity Framework migration schema" referenced in the comment?

    Thanks.

    Bob

    • Edited by Bob.at.SBS Tuesday, August 28, 2012 2:20 AM Still trying to get the numbered list to be formatted correctly.
    Tuesday, August 28, 2012 2:16 AM

Answers

  • You can find an article on code first migrations here: http://msdn.microsoft.com/en-us/data/jj591621

    There are also automatic migrations and an executable to run migrations, all of which have articles on the EF getting started page here: http://msdn.microsoft.com/en-us/data/ee712907


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by Bob.at.SBS Tuesday, August 28, 2012 9:36 PM
    Tuesday, August 28, 2012 8:37 PM
    Moderator

All replies

  • Hi Bob,

    The code is created this way to work around the following bug: http://entityframework.codeplex.com/workitem/23

    If you use the other methods of creating a database then the Migrations tables are created in the database that are used for code first migrations. Creating the database this way allows you to put your membership providers in the same database as your application and still use migrations.  


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Tuesday, August 28, 2012 5:27 PM
    Moderator
  • Thanks.  That touches on my third question: What are Migrations and where can I find documentation (or, what apparently passes for documentation nowadays, a blog entry) that teaches me about them?

    Bob

    Tuesday, August 28, 2012 7:37 PM
  • You can find an article on code first migrations here: http://msdn.microsoft.com/en-us/data/jj591621

    There are also automatic migrations and an executable to run migrations, all of which have articles on the EF getting started page here: http://msdn.microsoft.com/en-us/data/ee712907


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by Bob.at.SBS Tuesday, August 28, 2012 9:36 PM
    Tuesday, August 28, 2012 8:37 PM
    Moderator