So now that we have our data context added to our services container and we have our entity now what
we can do is create in a migration which will be used to scaffold our database.
So what we can do and in order to do this what we need to do is go into our terminal window and we'll
need to make sure that dot net is not running.
Otherwise we're going to get an error when we try and do this.
So do make sure that your API server is closed down and we need to make use of the dot net and Steve
framework command line tools for this.
And this is one of those things that Microsoft move around in almost every version of the framework
Internet call 2.0 we would need to add a package reference inside the sea as possible to make use of
it and what like two point one.
It came as part of the SDK and Internet core 3.0.
It's been removed again from the SDK but it's not a problem because what we can do is install at all.
And on today call 3.0 what we need to do is say dot net tool install and we'll install it globally.
So it's available for any projects that we create we have dot net core and then we just specify dot
net dash F and press return.
Now I've already got this tool installed but you will need to go ahead and install this.
So just to make sure it's installed if you just run dot net F on its own and just press return this
or bring up the Help commands that we can use for entity framework core.
So as long as you see this you're good to go and what we want to do here is actually see the into our
activities folder and then what we can do is run dot net ESF migrations and to add any migration and
we're going to give it a name of initial create.
Now because we're using multiple projects and we've got our data context inside our persistence project
and our startup project is our API we actually need to tell entity framework about both of these projects.
So what we need to do is give it a DSP for the persistence projects which tells it where our data context
is.
And we also need to let it know about our startup project which contains our connection strings.
So we'll specify Tash s and API and then make a press return.
And I've said MF Revlon dot net in this case so make sure we spot that correctly then press return and
what you should find is we get an error and sometimes errors can be a bit vague misleading the confusing
but this one is very very clear and exactly what we need today.
And it's telling us that our startup project doesn't reference Microsoft entity framework or design
and this package is required fully entity framework core tools to work.
So we need to go ahead and add another package.
So I'm just gonna copy license my clipboard and let's go ahead and open up the new get package manager
once again and we'll just paste in the package it needs Microsoft density framework core design press
return select the one that's made available.
And once again we want to go for the same vision that matches our runtime 3.0 point.
And we want to add this to our API projects just as it's telling us today and what slats complete them
will go for the restore option and once that is restored we go back to the terminal and let's give that
a never go and press return again and this time it creates our migration successfully.
So let's see what this is actually done for us and if I just close down the terminal window and we go
into solution explorer what we should find is the inside our persistence project.
Now we have a migrations folder and this is what entity framework portals have done for us.
Now there's three thousand here initially we've got an initial create dot c s which matches the name
of the migration and it's giving it a timestamp.
Now we also get another two files we get the data context model snapshot and the initial create designer
dot c s and the combination of these two files allows us to go back to an earlier migration if we were
to use the database update command inside dot net.
But the one we're interested in is less initial create dot.
Yes.
So let's take a look at this particular migration class and there's two methods inside here.
We have an up method and a down method and inside the up method what we have here is the command or
the code to create a table in our database.
And it's given a name of values.
And this matches the DB set we added to our data context file.
Now inside here we've got two columns being created an idea column and the name column.
And the idea column because it's an integer and it's our primary key.
And the reason it's our primary key is because entity framework is convention based.
If we give a name of one of our properties and I.D. name or the name of I.D. then it's going to automatically
assume that we want to use this as our primary key.
Now we can override that if we wanted to but we do want this to be our primary key in this case.
And what it's also going to do in this case is add an annotation for sequel lights to auto increment
every time we add a new entity to that particular table and that of course we've got our name column
here as well.
Now inside the down column is the opposite of what's going up in the Up column and in this case all
it's going to do is drop the table that was created.
And if we were to revert to an earlier migration then this would go ahead and call the code inside the
down method to drop that particular table.
Now we've got our migration in place so we're ready to create our database.
And what we're going to take a look at next is how we can do this inside our code so that we can create
our database and apply our migrations whenever we run our application and we'll take a look at that
next.