How can I disable the use of the __MigrationHistory table in Entity Framework 4.3 Code First?

前端 未结 2 1108
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 18:31

I\'m using Entity Framework 4.3 Code First with a custom database initializer like this:

public class MyContext : DbContext
{
    public MyContext()
    {
           


        
2条回答
  •  天命终不由人
    2020-12-17 19:18

    At first I was sure it was because you set the default initializer in the ctor but investigating a bit I found that the initializer isn't run when the context is created but rather when you query/add something for the first time.

    The provided initializer all check model compability so you are out of luck with them. You can easily make your own initializer like this instead though:

     public class Initializer : IDatabaseInitializer
        {
    
            public void InitializeDatabase(Context context)
            {
                if (!context.Database.Exists())
                {
                    context.Database.Create();
                    Seed(context);
                    context.SaveChanges();
                }
            }
    
            private void Seed(Context context)
            {
                throw new NotImplementedException();
            }
        }
    

    That shouldn't check compability and if the Database is missing it will create it.

    UPDATE: "Context" should be the type of your implementation of DbContext

提交回复
热议问题