Adding our First Migration
In this lesson, we'll be making our first migration
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 00:11] In the last lesson we added entity framework code to our project. If we go back we named our table courses since this is going to have all our course related properties.
[00:12 - 00:21] If we go back to our course entity class this is going to be our table columns. This is not the final version we will be making changes as we move along.
[00:22 - 00:32] Now to make the database appear in our project we will have to make a migration . Since we are talking about migration let me remind you that we are using code first approach.
[00:33 - 00:43] Because of which entity framework code is responsible for transforming your properties into a database table. Now to make a migration we will have to go back to our terminal.
[00:44 - 00:51] Let's go back from API project to the root of our project. To make migrations we need to have entity framework tools.
[00:52 - 01:05] Let's see if I have it type .NET tool list - - global. As you can see my tool list contains .NET EF.
[01:06 - 01:17] If you don't have it go to nuke.org/packages/dotnet-ef. You can see the command we need to install the tool.
[01:18 - 01:24] Make sure it matches your runtime or it will show you a warning. Now copy this command to your clipboard.
[01:25 - 01:32] Go back to the terminal and run this command. To me it says .NET EF is already installed.
[01:33 - 01:38] You won't see it if you are running this command for the first time. You can use the same command to update the tool.
[01:39 - 01:45] Just replace install with update. I won't do it since I am using the latest version right now.
[01:46 - 01:56] Let's run .NET tool list - - global again. If it's correctly installed you will see .NET -ef listed.
[01:57 - 02:09] To check which all features .NET EF tool provides us type .NET EF - edge. On top you see options out of which we already selected .edge.
[02:10 - 02:22] If you look at commands we can manage database, tb context and migrations using the tool. Since we are going to make a migration let's see which all commands we can use with migrations.
[02:23 - 02:33] Type .NET EF migrations .edge. We have an option to add, list, remove and to create a script.
[02:34 - 02:44] We will use the add command so let's clear it and type .NET EF migrations add. Let's call it first migration since it requires a name.
[02:45 - 03:14] It also needs to know the name of the project which is infrastructure and also name of our starter project which is API. Now our final command is .NET EF migrations add then the name of the migration which is first migration -b which stands for project and our project is infrastructure then -s which stands for the starter project which is our API project.
[03:15 - 03:25] Now our final command is .NET EF migrations add first migration -b infrastructure -s API. Now let's press enter.
[03:26 - 03:36] Oh no we see an error. It says your starter project API doesn't reference Microsoft.Entity framework code.design.
[03:37 - 03:45] This package is required for the entity framework code tools to work. Ensure your starter project is correct, install the package and try again.
[03:46 - 03:56] Alright it tells us what to do. We have to install Microsoft.Entity framework code.design package to our API project since that is our starter project.
[03:57 - 04:05] So let's go back to our command palette, open the nougat gallery and search for this package. Alright we found the package.
[04:06 - 04:24] Now click on it and install it in our API project. Once it is installed let's go back to our terminal and rerun the same command.
[04:25 - 04:34] Now we see success. It says entity framework code initialized store context using provider Microsoft.Entity framework code.sqlite.
[04:35 - 04:39] Perfect. Now let's see what it has created.
[04:40 - 04:46] As you can see we have a migrations folder inside our infrastructure project. It has created three files.
[04:47 - 04:57] The data context, model snapshots and the designer file. These files are just to allow entity framework to keep track of what migrations have been created so far.
[04:58 - 05:10] It helps to switch back easily to a previous migration. If we look at the first migration class it has derived from migration class which is the code entity framework class.
[05:11 - 05:25] Below that we have a migration builder which creates a table for us called courses as we mentioned in our store context file. It has mapped to all our course properties and if you see below it has made our ID property the primary key.
[05:26 - 05:39] We discussed that code first migrations are convention based and this class has gone with the convention and used our ID property as a primary key. This is usually the primary key in SQL tables as well.
[05:40 - 05:44] So we have two classes here. Up class and the down class.
[05:45 - 05:56] Up class created the table and down class dropped the table. Now we are done with the migration and in the next lecture we will create a database table based on this migration.
[05:57 - 05:57] So I'll see you in the next one.