我正在使用 Entity Framework 并在我的上下文中继承自 DbContext。
public class MyContext : DbContext, IMyContext
{
static MyContext()
{
Database.SetInitializer<MyContext>(null);
}
//other stuff
}
这一行的目的是什么?
Database.SetInitializer<ArContext>(null)
最佳答案
您可以关闭应用程序的数据库初始化程序。在不想丢失现有数据的生产环境中。在这种情况下,您可以关闭初始化器,如下所示。
public MyContext()
{
Database.SetInitializer<MyContext>(null);//Disable initializer
}
有四种不同的数据库初始化策略:
- CreateDatabaseIfNotExists: This is default initializer. As the name suggests, it will create the database if none exists as per the configuration. However, if you change the model class and then run the application with this initializer, then it will throw an exception.
- DropCreateDatabaseIfModelChanges: This initializer drops an existing database and creates a new database, if your model classes (entity classes) have been changed. So you don't have to worry about maintaining your database schema, when your model classes change.
- DropCreateDatabaseAlways: As the name suggests, this initializer drops an existing database every time you run the application, irrespective of whether your model classes have changed or not. This will be useful, when you want fresh database, every time you run the application, like while you are developing the application.
- Custom DB Initializer: You can also create your own custom initializer, if any of the above doesn't satisfy your requirements or you want to do some other process that initializes the database using the above initializer.
关于c# - 存储库中 Database.SetInitializer<ArContext>(null) 的用途?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39710784/