WEBVTT

00:00.290 --> 00:00.770
Okay.

00:00.770 --> 00:05.960
What we have at the moment is an empty database, and we want to correct that and have some demo products

00:05.960 --> 00:11.390
so that we can lay out our user interface when it comes to building our client application, so on and

00:11.390 --> 00:12.080
so forth.

00:12.080 --> 00:13.310
So let's deal with that.

00:13.310 --> 00:19.310
Now let's close down this store and inside our data folder let's create another new class.

00:19.310 --> 00:30.290
So I'm going to say new file select class and we'll call it DB initial iser and press return okay.

00:30.320 --> 00:36.980
So in this class then we're going to create some code so that we can do something with our DB context,

00:36.980 --> 00:40.790
because we're going to need to save a bunch of products into our database.

00:40.790 --> 00:46.640
And where we're going to use this DB initializer class is actually in our program class.

00:46.640 --> 00:48.680
So we're going to do something inside here.

00:48.680 --> 00:53.360
And we're going to create a method inside here and say public void.

00:53.360 --> 00:57.500
Because after public comes what are we going to return from this method.

00:57.500 --> 00:59.480
Well, we're not going to return anything from it.

00:59.480 --> 01:01.520
And I'm going to call it init db.

01:01.910 --> 01:07.210
And in here we're going to pass in as a parameter web application app.

01:07.900 --> 01:14.650
And the reason for that, let's just open up the program class at the same time as we want access to

01:14.680 --> 01:16.870
this effectively.

01:16.870 --> 01:22.510
And I'll explain why as we build this particular class so that we can seed some data in there.

01:23.020 --> 01:26.170
So we're going to use something called a using statement here.

01:26.170 --> 01:30.070
And I'm going to say var scope using var scope equals app.

01:30.430 --> 01:33.040
And this will give us access to our services.

01:33.040 --> 01:35.200
And I can say create scope.

01:35.200 --> 01:39.940
So what's this using statement all about then if well if we hover over this then we can see that this

01:39.940 --> 01:41.530
has got a dispose method.

01:41.530 --> 01:47.230
And this performs application defined tasks associated with freeing, releasing or resetting unmanaged

01:47.230 --> 01:48.070
resources.

01:48.070 --> 01:54.790
So what this effectively does is anything that we use inside scope, the thing that we've called scope

01:54.790 --> 02:00.190
is going to be disposed of by the framework after we've finished using it.

02:00.220 --> 02:02.200
And we don't need to clean up ourselves.

02:02.200 --> 02:06.530
Now, dotnet is very good at cleaning up resources that are no longer in use.

02:06.680 --> 02:12.500
But by using this using statement, we ensure this happens as soon as it possibly can, rather than

02:12.530 --> 02:17.090
something called a garbage collector coming along at some future point which we do not have control

02:17.090 --> 02:17.720
over.

02:18.140 --> 02:23.120
It will clean up any unused resources, but this just guarantees that after we finished with this resource,

02:23.120 --> 02:25.730
it will be cleaned up on our behalf.

02:25.760 --> 02:31.670
Then I'm going to create a variable called context and say var context equals scope dot service provider.

02:31.730 --> 02:35.210
And we're going to say get required service.

02:35.210 --> 02:38.990
And I'm going to specify the store context.

02:38.990 --> 02:42.500
And this is going to give us access to our store context.

02:42.530 --> 02:45.530
Now we have to do this in a slightly different way.

02:46.040 --> 02:52.400
And because this is a service that's been defined in our program class in this code, then we need to

02:52.430 --> 02:58.640
get hold of that service via the framework, via the scope that we've been created or we have created

02:58.640 --> 02:59.570
up here.

03:00.050 --> 03:03.200
So this gives us access to our database context.

03:03.200 --> 03:08.290
And then I'm going to create a method called seed data and pass in the context.

03:08.530 --> 03:13.360
And if we hover over the context, then we can see that this is a type of store context.

03:13.360 --> 03:17.050
And it's got the optional property there.

03:17.050 --> 03:22.840
And that's not ideal because we don't want to try and use this if it could be null.

03:23.110 --> 03:27.430
So after the or instead of the semicolon here let's just go to the next line.

03:27.430 --> 03:29.050
We'll add a double question mark.

03:29.050 --> 03:35.470
And this tells dotnet what to do if the context is null.

03:35.800 --> 03:41.170
So if it is null then we're just going to throw a new exception and we'll just say throw new invalid

03:41.200 --> 03:46.240
operation exception because we cannot see data if we don't have our context.

03:46.240 --> 03:51.820
And I'm just going to say failed to retrieve store context.

03:51.820 --> 03:54.100
And then we've got our seed data method.

03:54.100 --> 03:55.120
Now this doesn't exist yet.

03:55.120 --> 04:01.480
So let's put our cursor inside seed data and use the quick fix to generate method seed data.

04:01.480 --> 04:07.300
And inside here, because we've now got access to our context, what we can do is we can migrate our

04:07.300 --> 04:11.030
database or update our database inside code.

04:11.030 --> 04:12.410
So I'm going to specify context.

04:12.410 --> 04:14.660
And then we've got access to our database.

04:14.660 --> 04:18.200
And we can use the migrate method.

04:18.200 --> 04:22.010
And if we hover over the migrate method then it tells us what it's going to do.

04:22.040 --> 04:26.330
This is going to apply any pending migrations fuller context to the database.

04:26.330 --> 04:30.200
And it will create a database if it does not already exist.

04:30.860 --> 04:34.910
So this will create and apply migrations to a database.

04:34.940 --> 04:40.130
Now we're also going to check to see if we have any products already in our database.

04:40.130 --> 04:46.190
So we'll add an if statement and we'll say if context dot products dot any, then we are simply going

04:46.190 --> 04:49.430
to return from this method and do nothing with it.

04:49.430 --> 04:56.240
So this return statement here, this is going to stop the execution of any more code inside this method.

04:56.840 --> 04:59.570
And below this we're going to create a new list of products.

04:59.570 --> 05:03.110
And we can say var products equals new list.

05:03.200 --> 05:06.350
And the list is going to be a type of product.

05:06.350 --> 05:10.640
And inside here we open curly brackets.

05:10.640 --> 05:11.860
So I'll go on to the next line.

05:11.890 --> 05:16.780
Open curly brackets and inside here we can start defining a new list of products.

05:16.810 --> 05:23.020
So I could say for instance, new products and then open curly brackets.

05:23.020 --> 05:25.930
And then I could start populating the different fields.

05:25.930 --> 05:28.780
So I could say id one, I need to be a comma.

05:28.780 --> 05:31.600
We could say the name equals test.

05:31.660 --> 05:36.670
We could say description equals whatever and carry on going like this.

05:36.670 --> 05:37.810
But we need a bunch of products.

05:37.810 --> 05:38.950
We don't just need one.

05:38.950 --> 05:44.320
And it's very tedious to start typing out all of this seed type of code.

05:44.320 --> 05:46.210
So we're not going to do that.

05:46.240 --> 05:52.630
I have provided a snippet for us to use that has a bunch of products that we can use for demonstration

05:52.630 --> 05:53.350
purposes.

05:53.350 --> 05:56.650
So let's go to the course assets folder.

05:56.650 --> 06:02.230
Please go back to lesson three if you haven't already got this, and make sure you download the course

06:02.260 --> 06:02.710
assets.

06:02.710 --> 06:07.060
Inside here is a file called products dot txt.

06:07.090 --> 06:11.410
If we open this then this just has a bunch of new products.

06:11.410 --> 06:17.960
And if I just control a control C and go back to VSCode and paste this in.

06:17.960 --> 06:25.550
And then just add the closing semicolon there and make sure things look okay.

06:25.580 --> 06:29.390
Then we can see that we have a bunch of new products.

06:29.390 --> 06:32.090
The formatting didn't work quite how I wanted it to.

06:32.120 --> 06:38.090
I'd like it to be shifted over slightly to the left, and the auto formatting, which we should be able

06:38.090 --> 06:42.860
to access via right clicking, and then just say format document.

06:42.860 --> 06:45.200
And there is a shortcut key for this as well.

06:45.230 --> 06:48.110
That should have shifted things over to the left a bit, but it didn't.

06:48.110 --> 06:49.640
So I'll just do that manually.

06:49.640 --> 06:52.280
And then we've got our bunch of new products inside here.

06:52.280 --> 06:56.960
Now there is a hint here about, hey, this could be simplified.

06:56.960 --> 07:02.750
And if we do take the opportunity to look at how it can be simplified, we can use the use new.

07:02.750 --> 07:05.060
And there's also a fix all here.

07:05.060 --> 07:08.990
So let's take a look at using that and say document.

07:09.290 --> 07:12.470
And this simplifies our code and takes out all of that new product.

07:12.470 --> 07:16.780
And this is something that was available in dotnet eight just to remove some of the boilerplate.

07:16.780 --> 07:20.140
That's not necessary when creating a new instance of something.

07:20.140 --> 07:23.500
So there's a slightly cleaner way to do this now.

07:23.500 --> 07:28.690
So our VAR products, we want to save this list of products into our database.

07:28.690 --> 07:35.350
So below the list of products let's bring the cursor down below that below the semicolon.

07:35.350 --> 07:43.210
And we'll say context dot products dot add range so that we add the range of products in that list.

07:43.210 --> 07:45.400
And we'll specify products.

07:45.790 --> 07:49.180
And then we'll say context and save changes.

07:49.210 --> 07:54.850
And the way that Entity Framework works is everything here and upwards.

07:54.850 --> 07:58.330
All it's doing is it's tracking this list of products in memory.

07:58.330 --> 08:04.930
And it's only when we get to the context save changes that we actually make a request to the database

08:04.930 --> 08:06.100
at this point.

08:06.100 --> 08:07.990
So that's our seed class.

08:07.990 --> 08:10.750
In the next lesson we'll take a look at how we can use this.

08:10.750 --> 08:16.000
And we'll seed the data into our database and we'll make sure it exists.

08:16.000 --> 08:18.160
And we'll take a look at that next.
