How do I allow the EF4 CodeFirst database initializer to run under development, but not run in production

后端 未结 5 650
有刺的猬
有刺的猬 2021-01-01 07:06

I am attempting to deploy my first alpha version of a system online for a few people to start using. On development I make heavy use of the DropCreateDatabaseOnModelC

5条回答
  •  独厮守ぢ
    2021-01-01 07:14

    What about inversion of control:

    var initializer = container.Resolve>();
    Database.SetInitializer(initializer);
    

    Based on your IoC configuration you will either return developement or production initializer. You can have different configuration file for each build configuration so you can also have IoC container configured differently.

    public class ThrowExceptionInitializer : IDatabaseInitializer
    {
        public InitializeDatabase(Context context)
        {
            // Custom exception
            throw new InvalidVersionException("The new application version is not supported by the database version");
        }
    }
    

提交回复
热议问题