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

标签 c# .net entity-framework visual-studio-2015

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

相关文章:

c# - 正确使用 AutoCAD API 的 Document.SendStringToExecute 方法

c# - 混合 C#\C++ 应用程序中的 NLog

c# - C# 中的 Linkdemand 警告

缺少 cookie 时,.NET 核心返回 500 而不是 401

c# - 使用 Entity Framework 6.1 IndexAttribute 属性声明字符串字段键时指定最大长度?

c# - 将随机生成的颜色分配给值

c# - 如何在 C# 中的柱形图顶部添加百分比

c# - 用于编译的 .NET 绑定(bind)重定向

linq - Entity Framework 4在组合字段中搜索

c# - 实体数据源在哪里