In this section, I create the migration and update the database.
- Now that we've migrated the store context, it is time to update the database using EF Core Migrations. Migrations are vastly improved over their Entity Framework 6 counterpart. Instead of storing a hash of the database model in an EF Migrations History table, it actually stores the model in a C sharp class. This makes it much better working with teams. On a potential downside, the Configuration Seed method is removed, so you no longer have the opportunity to automatically seed the database when you run a migration.
We're going to start off, and this is just a habit I've gotten into, we're going to add a solution folder, and we'll just call it Solution Items. In this, we're going to add a text file, so Add New Item, Text File, we'll call it Migrations. This just helps me store the migrations. The first thing we need to do is change the directory in Package Manager Console to the directory for the SpyStore DAL project.
Migrations are a dotnet command line interface command, and if you recall, we added that CLI package reference into the SpyStore DAL project. We have to be in the same directory as that project file in order for the migrations to work. So we just change the directory. Do that with the change directory command, and then we call dotnet ef migrations add and a name for it, we'll just call it Initial.
Set the output directory, and we'll put it in the EF Migrations folder, just like our prior project that we're migrating, and we have to specify, well, to be clear, we don't have to specify the context. If we had more than one context, we would. We only have one, but I still like getting in the habit of always specifying the context. That's the fully qualified name, and then it built the migration.
Let's look, right here, we've got the Migrations folder. We've got the timestamp_Initial, and this new class, StoreContextModelSnapshot. If we take a peek at that, that is our database to find. We no longer have the configuration class like we did in EF 6, and that's where we lose the seed method. Final call we make is to update the database, so we just call dotnet ef database update.
It says it's done. Let's just confirm that. Go to our Databases. Refresh. We have our SpyStore Short Core, and in the Tables section, there are tables, EF Migration History table now just stores the fact that the migration has been run. Now that the migrations are complete, let's use this Migrations.txt file.
All I do in here, and this is just personal preference, is I copy in the commands so that if I have to recreate them for any other reason, or somebody comes along after me, looks at the code, they know exactly how to recreate the migrations.
- Creating the .NET Core project
- Adding Entity Framework
- Migrating the data access layer (DAL)
- Configuring services and the HTTP pipeline
- Adding remaining services to the dependency injection container
- Migrating controllers and actions
- Testing the services