Database initialization strategies

Creating the Database for the first time isn’t the only thing we need to worry about; for example, how can we keep track of the changes that will definitely occur to our Data Model?

In previous versions of EF (up to 6.x), we could choose between one of the Database management patterns (known as Database Initializers or DbInitializers) offered by the Code-First approach, that is, by picking the appropriate Database initialization strategy for our specific needs: CreateDatabaseIfNotExists, DropCreateDatabaseIfModelChanges, DropCreateDatabaseAlways, and MigrateDatabaseToLatestVersion. Additionally, should we need to address specific requirements, we can also set up our own custom initializer by extending one of the preceding and overriding their core methods.

The major flaw of DbInitializers was them not being immediate and streamlined enough for the average developer. They were viable, yet difficult to handle without an extensive knowledge of the whole Entity Framework logic.

In EF Core, the pattern has been greatly simplified; there are no DbInitializers, and automatic migrations have also been removed. The Database initialization aspect is now entirely handled through PowerShell commands, with the sole exception of a small set of commands that can be placed directly on the DbContext implementation constructor to partially automatize the process; they are as follows:

  • Database.EnsureCreated()
  • Database.EnsureDeleted()
  • Database.Migrate()

There’s currently no way to create migrations programmatically; they must be added via PowerShell, as we will see shortly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.184.200