Okay, so now that we've defined our DB context inside our program class, now we can supply the connection
string.
That Entity Framework needs to be able to make that connection to our database that we don't have yet,
but we will do soon.
So in order to provide this configuration where we get this from configuration, then we need to go
to one of our configuration files.
We've got Appsettings.json or App Settings development dot JSON.
Now since we're in development mode and we're creating our application, then configuration settings
that are not particularly private, such as our SQLite connection string can go in here.
I'll talk about secrets a bit later about things where we don't necessarily want them to be visible
to anybody else, but something like a SQLite connection string doesn't contain any password information
and is perfectly fine to put in something.
Even if we do put our code onto GitHub in a public repository, then we're okay with that information
being out there.
And like I say, I'll talk about what we do with more secure things, such as API keys that we might
also want to supply to our application.
I'll talk about that a bit later, but for now we're just getting things set up.
So we want to supply a connection string.
Now inside our app Settings development.
Jason, be careful about where you add this.
Try not to add it inside the logging section.
That will not work.
We need to add it below the logging section and these bracket colors.
Please do pay attention to these because we want it below.
In my case this pinky purple closing bracket.
And then we can open quotes and we can choose connection strings a little snippet that helps us there.
And this needs to match what we've supplied in our program class.
So I've called this default connection which is kind of conventional.
And that's what I'm going to supply inside our connection strings section inside here.
And then we add a colon.
And then we supply effectively a SQLite connection string.
So these are very simple.
It's just data source equals.
And I'm just going to call it store DB because a SQLite database is stored in a file.
Now, I've made a typo inside here that I do not get warned about.
And specifically I've used dates instead of data.
Now I don't get warned about this by the compiler.
It doesn't know what this string should be.
And in fact, strings you don't really get any help with from your compiler if you make a typo.
If you make a mistake, guaranteed, it's just going to persist in your application, and you're not
going to know until you create the database.
Because this will not work.
It needs to be data source.
So do watch out for typos when you are using strings.
It is something we have to use on occasions.
And like I say, you don't get any help from the compiler.
VS code isn't going to put any nasty squiggles under this, so please be careful.
And if something doesn't work, it's just the first place you come back to check.
Did I make a mistake in a string?
So now we have that in place, we need to go ahead and create a migration.
So we're going to open up the terminal is where we're going to do this.
But when it comes to creating a migration using the.
Net SDK, we actually need to add a tool to the SDK to help us do that.
And it doesn't come by default.
And if we just search for dotnet f nuget inside Google, then we can go to NuGet the sites.
And this will give us the command that we need to use to go ahead and install this into our SDK effectively.
So.
Net tool install global.
Net f version nine I'm just going to copy this command.
Go back to VS code and paste in this command.
It installs the tool globally.
And if you've already got it fine it's going to update it to the latest version in this case version
nine.
So I'll press return on that just to make sure this tool does get installed.
And in my case I already had a prior version.
So it's updated that from 8.08 to 9 9.0.0.
Great.
And if you want to see what other tools you have, you can use a command like dotnet tool list dash
G to see what global tools you have available.
And I can see that I've got dotnet f available now.
So very importantly before we do use this command that we're about to use, please ensure your dotnet
API server is stopped.
That is a requirement for what we're doing here and mine is not running.
So I'm safe to use this command.
And what we use here is we'll just make sure we've got dotnet f available.
So I'm just going to type in dotnet f press return.
And then we can see the help guide for Entity Framework command line tools.
And we can use commands related to the database related to the Dbcontext and also related to migrations
as well.
So I'm going to use dotnet f migrations at and I'm just going to call it initial create.
And I'm going to specify an output directory, because this is going to create some classes that define
the database schema based on the code that we've written so far.
So I'm going to output this into data forward slash migrations.
This is optional.
But if we do not supply list then it's just going to create a migrations folder in the root of our project.
And I'd rather keep data related code together.
So I'm going to press return.
And that should go ahead and create a migration for us.
Great.
It's now been done.
So if we go and take a look at what has been created and we go to our data folder inside here we've
got a migrations folder and we have a few files inside here we've got the store context model snapshot
and a designer file.
And these files are generated to help us roll back to an earlier migration.
So we're not interested in those.
We're interested in this one the date time followed by initial create CSS.
And this tells us what is going to happen when we apply this migration.
It's going to create a table called products.
It will also create a database if it has not already been created.
So it creates a table called products.
And it uses if I go back to the store context, it uses this name that we supply for the table name.
Great.
Because Entity Framework is convention based inside here we can see that we have a number of columns
that are going to be created inside the table.
Now the ID column this has got an annotation of SQLite autoincrement because it's called ID.
And because this is going to be used as the primary key and because it's an integer, then Entity Framework
knows that each time we add a new Productlist database, it's going to need a unique ID and it will
autoincrement.
So it will take the next available integer from what's already inside that table.
Also, we have our name, description, price, etc. and none of these can be nullable.
They've all got nullable false.
So we're not allowed to create a product that doesn't have a name, for example.
And also we have the primary key provided as the ID property based on the code that we've written.
So Entity Framework is very convention based.
We write minimal code.
And then Entity Framework is going to infer certain things based on the code that we've written.
And we've got two methods inside here.
We've got an up method for when we apply this to our database.
And we've got a down method as to what should happen if we want to remove this migration from our database.
So let's go ahead and apply this migration to our database that we do not have yet.
So still inside the API folder let's use another command from Entity Framework.
And we'll say dotnet f database updates.
And this is going to take a look at our migration.
Create the database and apply that migration to that database.
So I'll press return and we'll see what happens.
And we can see some activity in our logs.
And we can see that we're creating a table called products.
And we've got some other information going on here.
Creates another table called f migrations history.
So it can keep track of what migrations have already been applied to our database.
We can have more than one migration, and we will do as we create new entities.
And we want to make database schema changes.
We create more migrations so that the database can be updated with this new information.
And great.
Okay.
So let's take a look at our database as it stands at the moment.
Now another extension we installed earlier on was the SQLite viewer.
If we take a look at our folder view inside Solution Explorer we can see that we have store DB.
And if we click on this then we can see the contents of this database.
Now in order to click on this and see what I'm seeing here, you do have to have that extension, the
SQLite viewer.
If you do not have this then it will not display anything inside here.
It will just give you some kind of error.
So what we have inside here is we can see a bunch of tables.
We've got f migrations, history.
F migrations, lock SQLite sequence.
Don't worry about any of those tables, although you can take a look at the F migrations history, and
it tells you the migration that's been applied along with the version of Entity Framework that was used,
version nine.
And we've also got our products table, which is currently empty.
We don't have any products yet.
And inside here we can see the columns or the column names that have been created, the ID with the
primary key symbol, the name, description, price, picture, URL, etc..
So we've got our table created inside SQLite as well, and minimal effort to do this as well thanks
to Entity Framework.
But our table is empty and obviously we would like some demo products that our store is going to sell.
And to do that, we're going to need to seed some data into our database.
And we're going to take a look at how we do that next.