DevDiary



PM Console: Enable Migrations

Enable Migrations

Typing "enable-migrations" into the PM console enables migrations.

It creates a folder called "Migrations".

Inside the migrations folder is a configuration file.

The user can set automatic migrations and you can put your database seed code in the "seed override method".



Solution Explorer

Migrations/Configuration.cs



PM Console: Add Migration

The command "Add-Migration" followed by the name "initial" creates a migration with the name "initial".

NOTE: PM Conosle is not case sensitive.

Migrations/Initial.cs

A few files are created along including a class for the migration.

The date and time is put in the name of the files in UTC.

2021-03-04 (date), 17:02 (time), 321 (seconds).



PM Console: Add Migration

Migrations/Initial.cs



Migrations/Initial.cs

Migrations/Initial.cs

The intial class contains two methods, up and down.

The "Up" method contains code to update the database to be in sync wth the model that was used to generate this migration.

The "Down" method contains code to remove the updates that were applied in the "Up" method, so that the database will be in sync with the previous version of the model.



Migrations/Initial.cs: Up Method

Migrations/Initial.cs: Down Method



App.Config

Set Database Name

The app.config file contains the connection string.

The "Initial Catalog" parameter defines the name of the database the EF will generate.

The sceenshots below show "updating the database."

"update-databse" is typed into the package manager console.

It uses the initial migration to update the database.

Because there is no database at the moment the database is created.

Note that the database is empty because the seed method in the configuration class is empty.



PM Console: "update-database"

Empty Database Created



Configuration.cs: Seed

Configuration.cs: Seed



PM Console: "update-database"

Populate Database

Deleting the database and running the "update-database" command again.

The database is created again.

The seed method is executed whch populates the database with data.

The screenshots below show the contents of the tables.

It also shows the "program" code which just queries the database, retrieves all of the comicbooks and displays them.



ComicBook Table

Artist Table



Program.cs

Console Output



Artist Class

Add "Bio" Property

Added a new property to the "Artist" class.

"Bio" is a string.

The property has the data annotation [Required] which makes it non-nullable.

Making this change, means that the data model has been altered.



PM Console: Add-Migration

New Migration: "AddBioToArtist"

A new migration is created and named "AddBioToArtist".

In the screenshots below you can see that the new migration file has been added with a time-stamp.

You can also see the Up and Down method.



Solution Explorer

New Migration: "AddBioToArtist"



Configuration.cs

Update Seed Method

In the seed method, the Artist objects now need a "Bio".

For both of the artist objects in the seed method, the Bio property has been set to "TBD".

Without this, when updating the database, an error would occur because "Bio" is required.

In the screenshots below the command "update-database" is entered into the PM Console.

The "AddBioToArtist" migration is applied and the seed method is executed.

In the Artist table, you can see that a new column has appeared for the "Bio".



PM Console

Artist Table



Migration History Table

Downgrade to "Initial" Migration

Both migrations have been applied again.

To downgrade database, type "update-database -targetmigration Initial".

This reverts the "AddBioToArtist" migration.

Now the migration history table only shows the initial migration.

The next two screenshots show this process.



PM Console: Downgrade Database

Migration History Table



Artist Table

Downgrade to "Initial" Migration

The "Bio" column has been removed from the table.

In the "Artist" class the "Bio" property still exists.

The seed method, in the configuraion class, still contains code related to the "Bio" property.

The next two screenshots show this.



Artist Model

Configuration Class



Revert All Migrations

All Tables Removed From DB



Applying All Migrations

Migration History Table



ComicBook Table

Updating ComicBook Model

The "Average Rating" property in the comicbook class was replaced with "CBAverageRating" as ICollection.

Now, each comic book can have a history of average ratings.



ComicBookAverageRating Class

ComicBook Class



modelBuilder.Entity<>()

Context.cs

This part of the code had to be updated.



Configuration.cs

Comment Out Seed Method



PM Console: add-migration addCBAREntity

Add Migration (addCBAREntity)

These changes are saved by adding a migration called "addCBAREnitity".

Then the up() and down() methods inside the migration class file is altered.

The up() method populates the CBAverage table.

The down() method populates the ComicBook.AverageRating column.



Edit Up(): Added SQL(@"...")

Edit Down(): Added SQL(@"...")



PM Console: update-database

update-database

All of the migrations are applied.

The "Average Rating" column from the ComicBook table is removed.

The CBAverageRating Table is created.

Two rows were added manually to the CBAR table for ComicBookID = 1.

The database is then downgraded

The latest CBAverageRating value is transfered to the ComicBook.AverageRating column.



ComicBook Table

CBAverageRating Table



PM Console: Downgrade to AddBioToArtist

ComicBook Table