OK, now that we've got our data context file, we've set up our connection string, what we're going
to do next is effectively create a migration and migration is what we use when we want to create our
database based on the code that we actually have.
So this DB sense this is going to represent a table in our database and from the migration is going
to take the name of the table from the property name that we've given this CDB set here.
So the table is going to be called activities and the properties inside this table are going to be based
on the activity class.
And if we take a refresher what we have here, then we're going to have the seven columns.
These properties are going to be columns in our database table.
And because we've named the Identity Fields ID, then this is going to be used as our primary key in
our table.
And when we do something that we refer to as creating a migration, what this is going to do is effectively
create a script that we can use to create a database from.
So what we'll do and we'll need to do this in the middle of our application, so we'll also need to
stop our application from running.
So do make sure you do that before you attempt to do this.
Otherwise, you will for sure run into a problem and it will give you a very vague error, such as build
failed, but it won't tell you why it failed.
It won't say, hey, you can't do this because you've got your server running.
It's just going to give you a vague error to make sure your API server is stopped.
And what we'll need to do is clean up into the solution level itself.
Now, in order to create a migration, we use the dot net entity framework tools.
And in order to use this, we need to actually make sure we have them installed.
So what we can do is say dot net tool and then list.
And we're going to check global tools now, if you do not have this installed, then you will not see
what I've got inside here in my list.
Now, I already have Donitz if installed.
This is the tool that we need to use to create a new migration.
And if you run this tool and you do not see this in your list, then what you'll need to do, you'll
need to go to new gets the OK, look at packages and dot net dash EAF and this tells you the command
that we need to use to go ahead and install this particular tool.
Now the commands that it's showing here is for the latest version, five point zero point one of the
dot net if client side tool.
And I'm just going to copy this into my clipboard, go back to the code and just paste in this particular
command.
Now, it tells me there is an error here because I already have this installed, but that's fine, because
what I can do is swap install for updates.
And then this is going to go ahead and update my daughter, after all, to the latest version.
Now, if you don't have this installed, obviously the first command there is going to work for you.
If you've got an older version installed, then this command is going to work for you.
And now if I go ahead and list the tools available now, I can see that I'm on the latest version of
this tool.
Don't worry about the other tools I have here.
We're not using either of them, so I'm just going to clear the screen.
And what we need then is just to make sure that we have this tool available and we can say don't net
F, dash H.
And what we should see is the list of commands that were available for us to use from this particular
at all.
Now we've got commands to manage the database.
We've got commands to manage our DB context, and we've also got commands to manage migrations.
And we're going to use the migration's command here to create our migration based on the code that we've
written.
So I'm going to say don't let's if migration's and then we say adds and we'll just give it a name and
I'm going to say initial creates and then we specify the project where our DB context is.
So we need to replace a switch of Dasch P and then we can specify persistance because that's where our
data context is located.
And then we specify the starter project, which is going to be our API.
It needs both of these switches, otherwise we will run into an error.
So once we have that, we can go ahead and press return and it's going to go ahead and create a migration
for us, or it would do if we didn't run into an error.
Now, sometimes when we read into an error, the error description is so good, it tells us exactly
what we need to do.
And in this case, the error is so good it tells us exactly what we need to do.
It tells us to start a paper project does not reference something.
It needs to reference Microsoft identity framework design.
So we're going to copy this into my clipboard.
And this package is required for the entity, framework or tools to work.
And so you'll start a project is correct.
Install a package and try again.
Now, this error tells us everything that we need to do, so what I'm going to do is I'm going to open
up the new get gallery once again and I'm just going to paste in the package that we need.
I'm going to select this.
And the other told us we need this in our API project.
And that's what I'm going to do.
I'm just going to go ahead and install this and they should install no problems.
So I'm just going to press the button and the back in my terminal and I'm just going to rerun the Migration
Command.
And this time we get success and it tells us that and see framework for initialised data context using
our provider, which is siecle light with no options.
Great.
So what did this actually give us?
Well, let's go and take a look.
And if we open up Solution Explorer and take a look in our persistance project, then we've got a new
folder in here now called Migration's.
And this comes with three files.
We've got two files at the bottom here, the data context, model snapshots and the design of file.
And these files are just to allow entity framework to keep track of what migrations have been created
so far so that we can go back to an earlier migration if we wanted to.
But if we take a look at the initial create docs, this is the one that we're interested in, then we
can see that this is going to create or it has created a class called Initial Create that derives from
the migration class, which is an entity framework, core class we're inheriting from here.
And then we've got a migration builder.
And this job is to create a table and it's going to create a table collectivities with the following
columns.
And it's also got a constraint in here.
And it says that the primary key for this particular table is going to be that ID property that I mentioned
earlier.
Now, it's got nothing to do with the fact this was first in the list and it's got everything to do
with its name.
The fact it's called name.
If we call this thing, we would have run into an issue when we tried to create that migration.
Now we've got two methods in here.
We've got an up method.
This is what's going to be used when we create our database.
And we've got a down method.
What we do when we go back to an earlier migration, we simply drop this particular table.
So now we have our migration in place.
What we'll take a look at next is actually creating our database, using this migration.