The point of this article is to go over the different steps that were needed to add a new column to one of my existing table. This table was already used by my users using my application called My Stocks Alerts & Charts.
To add a new column or table to your existing DB you are going to need to create 3 new files. The first new class will be called MyAlertQuotesModelSnapshot which inherit from ModelSnapshot and have the annotation of DbContext, this will hold the new table schema model of our updated table.
Next, we will create a class called MyFirstMigration this will also hold you new table schema model, this class will not inherit from anything but will have the annotations of DbContext and Migration.
Lastly the most important part we are going to create the class that will add the column to our table this will be called MyFirstMigration will inherit from Migration and will allow us to add our new column to our table by using migrationBuilder.AddColumn as follows:
migrationBuilder.AddColumn<string>( name: "ExtraColumn", table: "MyAlertQuotes", nullable: true);
If you had wanted to add a new table that was called MyAlertQuotes we would have used migrationBuilder.CreateTable as follows:
Which would have added a new table to our DbSchema.
One last step, you will also have a class that inherits from DbContext, make you that you add check on initialization to make sure your users have migrated to the new Db schema
Here are my full classes: