WEBVTT

00:00.320 --> 00:03.170
Okay, so now we've got our DB initializer class.

00:03.170 --> 00:06.380
Let's just review this, make sure everything looks okay.

00:06.380 --> 00:08.300
And we've got our init db.

00:09.080 --> 00:13.160
And we create a scope using the using keyword.

00:13.250 --> 00:16.070
And we use the app services Create scope.

00:16.070 --> 00:22.820
Now when we using it this way and this services concept, it may become clearer once we start to look

00:22.850 --> 00:24.440
at dependency injection.

00:24.440 --> 00:29.660
Because what we're using here is a service that we've defined in our program class.

00:29.930 --> 00:34.490
And I mentioned earlier, albeit very briefly, about dependency injection.

00:34.490 --> 00:41.360
We haven't looked at that yet, and we cannot use it yet when we're doing something that involves the

00:41.360 --> 00:48.260
program class, because dependency injection isn't available until our application is actually running.

00:48.350 --> 00:54.170
Now, the point where we're going to use our DB initializer class is before our application has actually

00:54.170 --> 00:54.980
started.

00:54.980 --> 00:59.150
We're going to use this code just before we get to the run command.

00:59.750 --> 01:06.520
After we've run our application, then our services we can use for dependency injection Projection purposes,

01:06.520 --> 01:11.080
but we cannot do it where we're going to seed our data into the database.

01:11.650 --> 01:15.880
Now we've got another ellipses here to tell us that we can do something.

01:15.910 --> 01:21.730
And it tells us that member seed data does not access instance data and can be marked as static.

01:21.730 --> 01:22.840
So okay, fine.

01:22.840 --> 01:25.180
Let's do that and we'll use the ellipses.

01:25.690 --> 01:27.700
And can we use the ellipses.

01:27.730 --> 01:31.300
And well it's not letting us do anything automatically.

01:31.330 --> 01:34.840
So perhaps we just need to say private static void here.

01:34.840 --> 01:38.110
And then the warning or ellipses goes away.

01:38.110 --> 01:41.200
And the ellipses are not warnings or errors by Luella.

01:41.200 --> 01:45.730
Just advice to say, hey, you can do something slightly better here.

01:45.730 --> 01:50.920
And in this case, when it's saying it doesn't need access to instance data, it means we're not injecting

01:50.920 --> 01:52.660
anything into this class.

01:52.660 --> 01:59.170
Therefore, this can be marked as static because it doesn't depend on anything else.

01:59.170 --> 02:03.820
It's only depending or only using what's already inside this class.

02:03.820 --> 02:06.760
So this can be what's referred to as a static method.

02:06.760 --> 02:11.440
We'll get more into this syntax as we use it as we build our application.

02:11.530 --> 02:17.800
So after we've got access to the context, we then call the Setdata method, which will then migrate

02:17.800 --> 02:19.720
the database if necessary.

02:19.900 --> 02:21.880
It's going to check for any pending migrations.

02:21.880 --> 02:28.990
And then if it doesn't have a database then it will create the database and apply any pending migrations.

02:29.020 --> 02:30.730
And that's going to happen every time.

02:30.730 --> 02:34.690
So whenever we restart our app this method is always going to get called.

02:35.410 --> 02:37.150
Then we're going to check to see if we've got any products.

02:37.150 --> 02:40.000
And if we do have any products, we don't need to seed any more products in.

02:40.000 --> 02:42.400
So we're just returning out of this method.

02:42.400 --> 02:46.930
And then we create our new list of products and save them into our database.

02:47.350 --> 02:50.500
So we need to call this method inside our program class.

02:50.500 --> 02:54.700
And as mentioned we need to do this before our application is started.

02:54.700 --> 03:02.080
So in order to do that we can use DB initializer and get that into our program class.

03:02.080 --> 03:05.050
And we need to use the init db method.

03:05.080 --> 03:11.950
Now I don't have access to the init db method in here, because I also need to make this a static method.

03:12.430 --> 03:19.600
And a static method means effectively that I can use the method from this class without actually creating

03:19.600 --> 03:25.570
a new instance of this class, so I don't need to say new DB initializer.

03:25.570 --> 03:28.210
If I've got a static method in here that I'm calling.

03:28.210 --> 03:33.790
So if I go back to the program class now, I've got access to the init db method and I can just pass

03:33.790 --> 03:34.240
this.

03:34.240 --> 03:41.860
It's required parameter, which is the app that we're using inside the DB initializer, the web application

03:41.890 --> 03:42.310
app.

03:42.340 --> 03:45.820
Now we've got access to the init db.

03:45.940 --> 03:48.190
We can now go ahead and create our database.

03:48.190 --> 03:49.480
But we've already got a database.

03:49.480 --> 03:55.000
So let's remove that database so that we can see what happens when we restart our API.

03:55.030 --> 04:00.190
To make sure that the database gets created and we seed our data in there.

04:00.280 --> 04:02.140
So let's go back to the terminal.

04:02.140 --> 04:03.880
And I'm just going to clear the screen here.

04:03.880 --> 04:08.710
And please make sure your web application is stopped at this point.

04:08.710 --> 04:12.400
And we're going to use the Net AF database.

04:12.400 --> 04:16.570
And we can use the drop command which is going to remove our database.

04:16.570 --> 04:20.380
So I'll press return and we should get a confirmation saying are we sure.

04:20.410 --> 04:23.120
I'm going to say yes and press return.

04:23.120 --> 04:24.650
And this drops the database.

04:24.650 --> 04:32.690
And what that really means in the physical terms is our store DB is no longer part of our project files.

04:32.690 --> 04:40.010
And then in order to create that database and see the data because of this line of code, then we can

04:40.010 --> 04:41.030
just restart our app.

04:41.030 --> 04:46.430
So I'm just going to say well clear the window and say dotnet watch and we'll keep an eye on the terminal,

04:46.460 --> 04:49.250
the logs so that we can see what's happening.

04:49.250 --> 04:52.460
But what should happen and what we should see is something like this.

04:52.490 --> 04:59.780
We see a lot of inserts into products, which means our data is being seeded into the database.

04:59.780 --> 05:05.840
And we can further confirm that by taking a look at Solution Explorer, clicking on the store DB.

05:06.110 --> 05:10.550
And what we should see is that we've now got 18 products in our database.

05:10.550 --> 05:12.950
As easy as that.

05:12.950 --> 05:14.510
So great.

05:14.810 --> 05:21.830
And next let's see how we can use this data to return to query our database and return this data from

05:21.830 --> 05:26.930
the database in response to an HTTP request, which we'll take a look at next.
