[英]purpose of Database.SetInitializer<ArContext>(null) inside of repository?
I am using entity framework and in my context inheriting from DbContext. 我正在使用实体框架,并在我的上下文中继承自DbContext。
public class MyContext : DbContext, IMyContext
{
static MyContext()
{
Database.SetInitializer<MyContext>(null);
}
//other stuff
}
What is the purpose of this line? 这条线的目的是什么?
Database.SetInitializer<ArContext>(null)
You can turn off the DB initializer of your application. 您可以关闭应用程序的数据库初始化程序。 On the production environment where you don't want to lose the existing data.In such scenario you can turn off the initializer, as shown below.
在您不想丢失现有数据的生产环境中。在这种情况下,您可以关闭初始化程序,如下所示。
public MyContext()
{
Database.SetInitializer<MyContext>(null);//Disable initializer
}
There are four different database initialization strategies: 有四种不同的数据库初始化策略:
- CreateDatabaseIfNotExists: This is default initializer.
CreateDatabaseIfNotExists:这是默认的初始化程序。 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.
DropCreateDatabaseIfModelChanges:如果您的模型类(实体类)已更改,则此初始化程序将删除现有数据库并创建新数据库。 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.
DropCreateDatabaseAlways:顾名思义,无论您的模型类是否已更改,此初始化程序每次运行应用程序时都会删除现有数据库。 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.
自定义数据库初始化程序:您还可以创建自己的自定义初始化程序,如果上述任何一项不满足您的要求,或者您想要使用上述初始化程序执行其他初始化数据库的过程。
Reference : Database Initialization Strategies 参考: 数据库初始化策略
The default database initializer in Entity Framework Code First is CreateDatabaseIfNotExists
. Entity Framework Code First中的默认数据库初始化程序是
CreateDatabaseIfNotExists
。 As its name indicates, if the database does not exist it'll create it. 正如其名称所示,如果数据库不存在,它将创建它。
This behavior is good during development but when you go to production maybe you won't want to auto create your database. 这种行为在开发过程中很好,但是当你去生产时,你可能不想自动创建数据库。
If you want to disable the initializers you use the line you showed, so now you have full control over how the database will be created and evolve in time. 如果要禁用初始化程序,则使用显示的行,现在您可以完全控制数据库的创建方式和进度。
Other initializers:
其他初始化器:
DropCreateDatabaseIfModelChanges.
DropCreateDatabaseAlways
Custom DB Initializer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.