gpt4 book ai didi

c# - 存储库中 Database.SetInitializer(null) 的用途?

转载 作者:太空狗 更新时间:2023-10-30 00:01:18 25 4
gpt4 key购买 nike

我正在使用 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
}

有四种不同的数据库初始化策略:

  1. 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.
  2. 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.
  3. 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.
  4. 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.

引用:Database Initialization Strategies

关于c# - 存储库中 Database.SetInitializer<ArContext>(null) 的用途?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39710784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com