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".
The command "Add-Migration" followed by the name "initial" creates a migration with the name "initial".
NOTE: PM Conosle is not case sensitive.
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).
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.
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.
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.
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.
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.
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".
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.
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.
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.
This part of the code had to be updated.
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.
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.