存储库内部Database.SetInitializer< ArContext>(null)的目的是什么? [英] purpose of Database.SetInitializer<ArContext>(null) inside of repository?

查看:42
本文介绍了存储库内部Database.SetInitializer< ArContext>(null)的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架,并且在我的上下文中是从DbContext继承的.

I am using entity framework and in my context inheriting from DbContext.

public class MyContext : DbContext, IMyContext
{
    static MyContext()
    {
        Database.SetInitializer<MyContext>(null);
    }
    //other stuff
}

此行的目的是什么?

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
    }

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

  1. CreateDatabaseIfNotExists::这是默认的初始化程序.作为名字建议,如果没有数据库,则将创建数据库配置.但是,如果您更改模型类,然后运行带有该初始化程序的应用程序,那么它将抛出一个例外.
  2. DropCreateDatabaseIfModelChanges:此初始化程序删除现有的数据库,并创建一个新数据库(如果您的模型是类(实体类)已更改.所以您不必担心当模型类更改时,维护数据库架构.
  3. DropCreateDatabaseAlways :顾名思义,此初始化程序每次您运行该应用程序时,都会删除一个现有数据库,不管您的模型类是否已更改.这个当您想要新的数据库时,每次运行应用程序,就像在开发应用程序时一样.
  4. 自定义数据库初始化程序:,您还可以创建自己的自定义数据库初始化程序,如果以上任何一项都不满足您的要求或者您想要执行其他一些初始化数据库的过程使用上面的初始化程序.
  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.

参考资料: 数据库初始化策略

Reference : Database Initialization Strategies

这篇关于存储库内部Database.SetInitializer&lt; ArContext&gt;(null)的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆