WEBVTT

00:00.000 --> 00:06.570
Okay, so now that we've defined our DB context inside our program class, now we can supply the connection

00:06.570 --> 00:07.020
string.

00:07.020 --> 00:13.050
That Entity Framework needs to be able to make that connection to our database that we don't have yet,

00:13.050 --> 00:14.580
but we will do soon.

00:14.580 --> 00:20.520
So in order to provide this configuration where we get this from configuration, then we need to go

00:20.520 --> 00:22.500
to one of our configuration files.

00:22.500 --> 00:26.460
We've got Appsettings.json or App Settings development dot JSON.

00:26.460 --> 00:31.560
Now since we're in development mode and we're creating our application, then configuration settings

00:31.560 --> 00:37.530
that are not particularly private, such as our SQLite connection string can go in here.

00:37.530 --> 00:43.830
I'll talk about secrets a bit later about things where we don't necessarily want them to be visible

00:43.860 --> 00:49.860
to anybody else, but something like a SQLite connection string doesn't contain any password information

00:49.860 --> 00:52.710
and is perfectly fine to put in something.

00:52.710 --> 00:59.910
Even if we do put our code onto GitHub in a public repository, then we're okay with that information

00:59.910 --> 01:01.080
being out there.

01:01.080 --> 01:07.660
And like I say, I'll talk about what we do with more secure things, such as API keys that we might

01:07.660 --> 01:09.670
also want to supply to our application.

01:09.700 --> 01:13.330
I'll talk about that a bit later, but for now we're just getting things set up.

01:13.330 --> 01:15.880
So we want to supply a connection string.

01:15.880 --> 01:17.710
Now inside our app Settings development.

01:17.860 --> 01:20.290
Jason, be careful about where you add this.

01:20.380 --> 01:22.570
Try not to add it inside the logging section.

01:22.570 --> 01:23.380
That will not work.

01:23.380 --> 01:27.280
We need to add it below the logging section and these bracket colors.

01:27.280 --> 01:30.010
Please do pay attention to these because we want it below.

01:30.010 --> 01:33.580
In my case this pinky purple closing bracket.

01:33.580 --> 01:39.790
And then we can open quotes and we can choose connection strings a little snippet that helps us there.

01:39.790 --> 01:43.600
And this needs to match what we've supplied in our program class.

01:43.600 --> 01:47.500
So I've called this default connection which is kind of conventional.

01:47.500 --> 01:53.320
And that's what I'm going to supply inside our connection strings section inside here.

01:53.320 --> 01:54.640
And then we add a colon.

01:54.640 --> 01:58.540
And then we supply effectively a SQLite connection string.

01:58.540 --> 02:00.100
So these are very simple.

02:00.100 --> 02:02.170
It's just data source equals.

02:02.180 --> 02:08.900
And I'm just going to call it store DB because a SQLite database is stored in a file.

02:08.930 --> 02:14.600
Now, I've made a typo inside here that I do not get warned about.

02:14.630 --> 02:19.400
And specifically I've used dates instead of data.

02:19.790 --> 02:22.010
Now I don't get warned about this by the compiler.

02:22.010 --> 02:23.840
It doesn't know what this string should be.

02:23.870 --> 02:28.730
And in fact, strings you don't really get any help with from your compiler if you make a typo.

02:28.730 --> 02:33.680
If you make a mistake, guaranteed, it's just going to persist in your application, and you're not

02:33.680 --> 02:36.380
going to know until you create the database.

02:36.380 --> 02:37.640
Because this will not work.

02:37.640 --> 02:39.950
It needs to be data source.

02:40.220 --> 02:43.820
So do watch out for typos when you are using strings.

02:43.820 --> 02:46.070
It is something we have to use on occasions.

02:46.070 --> 02:48.650
And like I say, you don't get any help from the compiler.

02:48.680 --> 02:54.590
VS code isn't going to put any nasty squiggles under this, so please be careful.

02:54.620 --> 02:58.130
And if something doesn't work, it's just the first place you come back to check.

02:58.130 --> 03:00.620
Did I make a mistake in a string?

03:01.010 --> 03:04.650
So now we have that in place, we need to go ahead and create a migration.

03:04.650 --> 03:07.830
So we're going to open up the terminal is where we're going to do this.

03:08.250 --> 03:12.780
But when it comes to creating a migration using the.

03:12.960 --> 03:18.060
Net SDK, we actually need to add a tool to the SDK to help us do that.

03:18.060 --> 03:20.040
And it doesn't come by default.

03:20.040 --> 03:28.980
And if we just search for dotnet f nuget inside Google, then we can go to NuGet the sites.

03:28.980 --> 03:37.200
And this will give us the command that we need to use to go ahead and install this into our SDK effectively.

03:37.200 --> 03:37.440
So.

03:37.590 --> 03:39.660
Net tool install global.

03:39.900 --> 03:43.170
Net f version nine I'm just going to copy this command.

03:43.410 --> 03:46.140
Go back to VS code and paste in this command.

03:46.140 --> 03:47.700
It installs the tool globally.

03:47.700 --> 03:53.760
And if you've already got it fine it's going to update it to the latest version in this case version

03:53.760 --> 03:54.330
nine.

03:54.330 --> 03:57.840
So I'll press return on that just to make sure this tool does get installed.

03:57.840 --> 03:59.970
And in my case I already had a prior version.

03:59.970 --> 04:04.220
So it's updated that from 8.08 to 9 9.0.0.

04:04.250 --> 04:04.880
Great.

04:04.880 --> 04:11.450
And if you want to see what other tools you have, you can use a command like dotnet tool list dash

04:11.450 --> 04:14.000
G to see what global tools you have available.

04:14.000 --> 04:17.180
And I can see that I've got dotnet f available now.

04:17.180 --> 04:23.810
So very importantly before we do use this command that we're about to use, please ensure your dotnet

04:23.840 --> 04:26.420
API server is stopped.

04:26.420 --> 04:31.370
That is a requirement for what we're doing here and mine is not running.

04:31.370 --> 04:33.620
So I'm safe to use this command.

04:33.620 --> 04:38.660
And what we use here is we'll just make sure we've got dotnet f available.

04:38.660 --> 04:41.120
So I'm just going to type in dotnet f press return.

04:41.120 --> 04:45.650
And then we can see the help guide for Entity Framework command line tools.

04:45.650 --> 04:52.460
And we can use commands related to the database related to the Dbcontext and also related to migrations

04:52.460 --> 04:53.780
as well.

04:53.780 --> 05:02.930
So I'm going to use dotnet f migrations at and I'm just going to call it initial create.

05:03.260 --> 05:09.910
And I'm going to specify an output directory, because this is going to create some classes that define

05:09.940 --> 05:14.050
the database schema based on the code that we've written so far.

05:14.080 --> 05:18.400
So I'm going to output this into data forward slash migrations.

05:18.820 --> 05:19.960
This is optional.

05:19.960 --> 05:26.020
But if we do not supply list then it's just going to create a migrations folder in the root of our project.

05:26.020 --> 05:29.170
And I'd rather keep data related code together.

05:29.200 --> 05:30.430
So I'm going to press return.

05:30.430 --> 05:34.810
And that should go ahead and create a migration for us.

05:34.840 --> 05:35.530
Great.

05:35.530 --> 05:36.400
It's now been done.

05:36.400 --> 05:42.730
So if we go and take a look at what has been created and we go to our data folder inside here we've

05:42.730 --> 05:48.910
got a migrations folder and we have a few files inside here we've got the store context model snapshot

05:48.910 --> 05:50.950
and a designer file.

05:50.950 --> 05:55.720
And these files are generated to help us roll back to an earlier migration.

05:55.720 --> 05:57.640
So we're not interested in those.

05:57.640 --> 06:03.520
We're interested in this one the date time followed by initial create CSS.

06:03.520 --> 06:08.570
And this tells us what is going to happen when we apply this migration.

06:08.570 --> 06:10.910
It's going to create a table called products.

06:10.940 --> 06:15.200
It will also create a database if it has not already been created.

06:15.230 --> 06:17.750
So it creates a table called products.

06:18.350 --> 06:27.320
And it uses if I go back to the store context, it uses this name that we supply for the table name.

06:27.950 --> 06:28.790
Great.

06:29.090 --> 06:35.600
Because Entity Framework is convention based inside here we can see that we have a number of columns

06:35.600 --> 06:37.670
that are going to be created inside the table.

06:37.700 --> 06:45.830
Now the ID column this has got an annotation of SQLite autoincrement because it's called ID.

06:46.160 --> 06:52.460
And because this is going to be used as the primary key and because it's an integer, then Entity Framework

06:52.460 --> 06:57.920
knows that each time we add a new Productlist database, it's going to need a unique ID and it will

06:57.920 --> 06:58.820
autoincrement.

06:58.820 --> 07:03.890
So it will take the next available integer from what's already inside that table.

07:04.070 --> 07:09.160
Also, we have our name, description, price, etc. and none of these can be nullable.

07:09.190 --> 07:10.480
They've all got nullable false.

07:10.480 --> 07:14.860
So we're not allowed to create a product that doesn't have a name, for example.

07:14.860 --> 07:20.950
And also we have the primary key provided as the ID property based on the code that we've written.

07:20.950 --> 07:24.010
So Entity Framework is very convention based.

07:24.010 --> 07:25.450
We write minimal code.

07:25.450 --> 07:30.160
And then Entity Framework is going to infer certain things based on the code that we've written.

07:30.160 --> 07:32.140
And we've got two methods inside here.

07:32.140 --> 07:35.110
We've got an up method for when we apply this to our database.

07:35.110 --> 07:42.160
And we've got a down method as to what should happen if we want to remove this migration from our database.

07:42.190 --> 07:47.110
So let's go ahead and apply this migration to our database that we do not have yet.

07:47.110 --> 07:52.810
So still inside the API folder let's use another command from Entity Framework.

07:52.810 --> 07:56.800
And we'll say dotnet f database updates.

07:56.800 --> 07:59.290
And this is going to take a look at our migration.

07:59.290 --> 08:03.190
Create the database and apply that migration to that database.

08:03.190 --> 08:06.290
So I'll press return and we'll see what happens.

08:06.560 --> 08:08.690
And we can see some activity in our logs.

08:08.690 --> 08:11.900
And we can see that we're creating a table called products.

08:11.900 --> 08:14.930
And we've got some other information going on here.

08:14.960 --> 08:17.630
Creates another table called f migrations history.

08:17.630 --> 08:22.640
So it can keep track of what migrations have already been applied to our database.

08:22.640 --> 08:27.050
We can have more than one migration, and we will do as we create new entities.

08:27.050 --> 08:29.600
And we want to make database schema changes.

08:29.630 --> 08:35.720
We create more migrations so that the database can be updated with this new information.

08:35.750 --> 08:37.100
And great.

08:37.130 --> 08:37.610
Okay.

08:37.610 --> 08:41.690
So let's take a look at our database as it stands at the moment.

08:41.960 --> 08:46.280
Now another extension we installed earlier on was the SQLite viewer.

08:46.280 --> 08:53.390
If we take a look at our folder view inside Solution Explorer we can see that we have store DB.

08:53.390 --> 08:58.490
And if we click on this then we can see the contents of this database.

08:58.520 --> 09:04.010
Now in order to click on this and see what I'm seeing here, you do have to have that extension, the

09:04.010 --> 09:04.910
SQLite viewer.

09:04.910 --> 09:08.920
If you do not have this then it will not display anything inside here.

09:08.920 --> 09:11.050
It will just give you some kind of error.

09:11.080 --> 09:13.870
So what we have inside here is we can see a bunch of tables.

09:13.870 --> 09:15.370
We've got f migrations, history.

09:15.400 --> 09:17.920
F migrations, lock SQLite sequence.

09:17.950 --> 09:23.080
Don't worry about any of those tables, although you can take a look at the F migrations history, and

09:23.080 --> 09:29.290
it tells you the migration that's been applied along with the version of Entity Framework that was used,

09:29.290 --> 09:30.160
version nine.

09:30.160 --> 09:32.950
And we've also got our products table, which is currently empty.

09:32.980 --> 09:34.990
We don't have any products yet.

09:35.020 --> 09:40.870
And inside here we can see the columns or the column names that have been created, the ID with the

09:40.870 --> 09:45.010
primary key symbol, the name, description, price, picture, URL, etc..

09:45.370 --> 09:52.090
So we've got our table created inside SQLite as well, and minimal effort to do this as well thanks

09:52.090 --> 09:53.470
to Entity Framework.

09:53.470 --> 10:00.670
But our table is empty and obviously we would like some demo products that our store is going to sell.

10:00.700 --> 10:04.420
And to do that, we're going to need to seed some data into our database.

10:04.420 --> 10:07.570
And we're going to take a look at how we do that next.
