WEBVTT

00:00.020 --> 00:03.560
Okay, now it's time to take a look at using Entity Framework.

00:03.560 --> 00:08.240
Now that we have discussed it, and the first thing we're going to do is install the package that we

00:08.240 --> 00:10.430
need to make use of this.

00:10.430 --> 00:12.290
So we're going to go to our extensions.

00:12.290 --> 00:17.540
One of them that we installed earlier on that you should have is NuGet Gallery.

00:17.540 --> 00:21.980
So please do make sure you have this extension installed and available.

00:21.980 --> 00:23.900
And the way that we use NuGet Gallery.

00:23.900 --> 00:29.120
If you open up your terminal and let's just clear that window, then what you'll find is you've got

00:29.150 --> 00:30.890
a tab here for NuGet.

00:30.890 --> 00:37.040
And this is how we go ahead and install NuGet packages into our project.

00:37.040 --> 00:42.800
What we want to search for here is Microsoft Dot Entity Framework Core.

00:43.310 --> 00:48.650
And it should go ahead and search similarly named packages.

00:48.650 --> 00:52.940
And lest we've made a mistake and I've spelt Microsoft wrong.

00:52.940 --> 00:55.760
So that should be Microsoft Entity Framework Core.

00:55.760 --> 00:59.570
And this should bring up a list of packages related to that.

00:59.780 --> 01:06.470
Now we actually need two packages from this we we need Microsoft Entity Framework core dot SQLite.

01:06.500 --> 01:10.580
Please install the package with the same name.

01:10.580 --> 01:13.340
Don't install a similarly named one such as SQLite core.

01:13.370 --> 01:14.450
That's not going to work for you.

01:14.480 --> 01:18.140
You're going to get some strange errors when we start to use this.

01:18.140 --> 01:21.770
So please install Microsoft Entity Framework Core SQLite.

01:22.190 --> 01:26.390
Please pick the version that matches the major version of the runtime.

01:26.390 --> 01:29.360
In my case, it's going to be version nine of this package.

01:29.630 --> 01:34.760
If you're on dotnet nine, then just pick the the one that relates to what you're using.

01:34.790 --> 01:38.840
Of course, if you're on dotnet ten you would select version ten of this.

01:38.870 --> 01:41.900
It should be updated in line with dotnet as well.

01:41.900 --> 01:48.350
So let's go ahead and add this by using the plus button and sending it to or installing it into API

01:48.350 --> 01:49.550
Csproj.

01:49.910 --> 01:57.860
And what we also need is Microsoft Entity Framework Core Design, which we need to install so that we

01:57.860 --> 02:01.320
can create migrations with Entity Framework.

02:01.320 --> 02:04.650
So we're also going to install this one into API.

02:04.680 --> 02:06.900
Csproj as well.

02:07.500 --> 02:13.770
And once those have been installed, then what you should be able to do is go to Solution Explorer.

02:13.800 --> 02:21.060
Click on API and you should see the newly added packages Microsoft Entity Framework, Core Design and

02:21.060 --> 02:23.940
Microsoft Entity Framework Core SQL Lite.

02:23.940 --> 02:27.960
So please double check you have these installed.

02:28.200 --> 02:33.600
Now that we have this, we can make use of Entity Framework classes inside our project.

02:33.600 --> 02:37.890
And we're going to go to Solution Explorer inside the API.

02:37.920 --> 02:40.380
Let's just right click and say New Folder.

02:40.380 --> 02:45.180
And we're going to call this one data for all data related code.

02:45.180 --> 02:47.640
And we're going to create a new file inside here.

02:47.640 --> 02:51.600
And we're going to call it store context and not a new file.

02:51.600 --> 02:57.570
We need to create a new class called Store Context and press return.

02:57.600 --> 03:04.650
Now this class is going to derive from an entity framework class called Dbcontext.

03:04.650 --> 03:10.950
So the way that we derive from another class is we add a colon after the name of the class, and we're

03:10.950 --> 03:13.080
going to say dbcontext.

03:13.320 --> 03:17.430
And this should bring in the available options.

03:17.430 --> 03:21.180
And we want Dbcontext that's coming from Microsoft Entity Framework Core.

03:21.270 --> 03:27.750
So I'll press return to select that and we get the using statement added at the top so that this class,

03:27.750 --> 03:31.590
this Dbcontext class is coming from list namespace.

03:31.950 --> 03:32.610
That's fine.

03:32.610 --> 03:39.330
Now in order to use the Dbcontext and provide it with things like the connection string to our database

03:39.360 --> 03:43.890
or our database server, we need to provide this with a constructor.

03:44.160 --> 03:50.910
So a constructor in C-sharp world is when we create a new instance of the store context, then it runs

03:50.910 --> 03:52.890
the code inside the constructor.

03:52.890 --> 03:59.460
And the way that we create a constructor is we say Ctor to get the snippet for a constructor, and then

03:59.460 --> 04:04.070
we tell it what parameters, if any, that it needs, that we need to provide.

04:04.070 --> 04:08.960
When we create a new instance of this store context and the parameters that we do need to provide for

04:08.960 --> 04:09.140
this.

04:09.170 --> 04:14.450
We need to provide this with some DB context options and say options.

04:14.450 --> 04:16.970
But there's a better way of creating a constructor.

04:17.000 --> 04:19.310
Now in C-sharp eight and above.

04:19.310 --> 04:22.820
And we get these ellipses underneath the store context.

04:22.820 --> 04:27.650
And it tells us that we can use a primary constructor.

04:27.650 --> 04:33.620
So if we go ahead and use this, and what we have available to us is a quick fix, when we see this

04:33.620 --> 04:40.070
light bulb or we see ellipses or we see an underline in our code, we can hover over this and we can

04:40.250 --> 04:45.860
either select the quick fix here, we can use the light bulb here, or we can use the shortcut key combination,

04:45.860 --> 04:48.110
which in my case is command and period.

04:48.170 --> 04:50.030
And this will open up a menu.

04:50.030 --> 04:52.280
And it will give us an option of what to use.

04:52.280 --> 04:54.890
And we can use the primary constructor.

04:54.890 --> 04:58.190
And then we pass in the options.

04:58.190 --> 05:03.970
Now these options have to be passed to the class that we're deriving from.

05:03.970 --> 05:11.080
So as well as Dbcontext here, we need to add parentheses and specify options so that these options,

05:11.080 --> 05:17.500
which is going to contain our connection string, gets passed up to the Entity Framework Dbcontext.

05:17.530 --> 05:22.030
Now if we hover over the Dbcontext then this gives us documentation about the Dbcontext.

05:22.060 --> 05:25.330
Although because I've specified options, let's just remove that for the time being.

05:25.360 --> 05:30.250
Hover over the Dbcontext just to get a description about what this class actually does.

05:30.550 --> 05:38.170
Now this represents a session with the database and can be used to query and save instances of our entities.

05:38.170 --> 05:44.380
So when it comes to saving a product into the database or querying for a product in the database, then

05:44.380 --> 05:48.850
we use it via our store context which derives from the Dbcontext.

05:49.150 --> 05:54.010
Now, if you've done some coding before, you may well have heard of the unit of work and repository

05:54.010 --> 05:54.850
patterns.

05:54.880 --> 05:59.650
The Dbcontext is a combination of these patterns.

05:59.680 --> 06:05.950
Now we can also use them inside our project if we want to, or because this is a combination of those

06:05.950 --> 06:09.550
two patterns, we don't actually have to use them in our project.

06:09.580 --> 06:14.290
Now, I have covered those patterns on different courses, so I'm not going to cover that on this one.

06:14.320 --> 06:16.240
I'm going to keep things nice and simple.

06:16.240 --> 06:22.510
And because the Dbcontext is already a combination of these two patterns, we don't have to use it in

06:22.510 --> 06:23.860
our project.

06:23.890 --> 06:25.420
So that's the dbcontext.

06:25.420 --> 06:27.040
And we also need to specify.

06:27.070 --> 06:28.360
Well let's put the options back in.

06:28.390 --> 06:31.180
Now I've just got the description from that.

06:31.210 --> 06:35.020
We also need to specify inside here our different entities.

06:35.020 --> 06:37.330
And we do that we create a property.

06:37.330 --> 06:40.330
And this is going to be a type of dbset.

06:40.450 --> 06:49.750
And if we hover over the dbset then this is or can be used to query and save instances of our entity.

06:49.750 --> 06:56.770
And Linq queries against a DB set will be translated into queries against the database.

06:56.770 --> 07:03.050
So what this means is when we use Entity Framework is we write our code in C-sharp, which SQL doesn't

07:03.050 --> 07:12.320
understand, and then Entity Framework translates those C-sharp or that C-sharp code into SQL commands

07:12.320 --> 07:14.420
that our database does understand.

07:14.450 --> 07:16.070
So we only need to use one language.

07:16.070 --> 07:19.220
We don't need to know C-sharp and SQL syntax.

07:19.220 --> 07:24.080
We just need to know C-sharp to be able to query our database.

07:24.440 --> 07:26.540
Now our DB set takes a type.

07:26.540 --> 07:29.390
So we put the type of thing in angle brackets.

07:29.390 --> 07:31.490
And this is going to be a type of product.

07:31.520 --> 07:32.750
So I'll press return.

07:32.750 --> 07:35.330
And then we give our property a name.

07:35.450 --> 07:37.460
And I'm going to call it products.

07:37.460 --> 07:42.770
And because we're getting the warning here because of the type of property, this is being a DB set.

07:42.800 --> 07:47.510
It says must contain a non-null value or consider adding a required modifier.

07:47.540 --> 07:49.100
This actually caught me by surprise.

07:49.100 --> 07:54.380
This must have been recently added to Entity Framework version nine or dotnet nine, because we didn't

07:54.380 --> 07:57.230
used to have to add the required modifier here.

07:57.230 --> 07:59.210
So I'm going to see how this goes.

07:59.210 --> 08:00.560
It maybe it will cause us a problem.

08:00.560 --> 08:01.250
Maybe it will not.

08:01.280 --> 08:06.200
I was not expecting to need to use that inside this particular class anyway.

08:06.230 --> 08:10.040
Now that we have this available in here, we need to tell our startup code about this.

08:10.070 --> 08:18.710
Now our startup code is contained inside the Program.cs, and our Dbcontext class is something that

08:18.710 --> 08:23.840
we're going to want to use in other parts of our application so that we can query our database.

08:23.870 --> 08:29.660
So for that reason, we add this as a service inside our program class.

08:29.660 --> 08:32.660
And we're going to specify builder dot services.

08:32.660 --> 08:38.180
And there's a special method inside here to add our dbcontext.

08:38.300 --> 08:41.840
Inside the angle brackets we specify store context.

08:41.930 --> 08:44.600
And then we provide it with the options.

08:44.600 --> 08:47.300
And the options are what we specify here.

08:47.300 --> 08:50.900
So we need to pass this to our store context.

08:50.900 --> 08:56.480
Because when we add this as a service the framework will create a new instance of our store context.

08:56.510 --> 09:00.450
So it needs to know about the options we're going to provide to it.

09:00.450 --> 09:02.580
So back to our program class we're going to specify.

09:02.610 --> 09:03.360
Opt.

09:03.510 --> 09:06.420
And then we provide this as an expression.

09:06.420 --> 09:10.950
So we effectively add an arrow with the equals and angle brackets.

09:10.950 --> 09:15.450
And then before the closing parentheses we'll add curly brackets.

09:15.450 --> 09:18.510
And we close off the statement with a semicolon.

09:18.510 --> 09:24.210
And then for the options we can say options and use SQL Lite.

09:24.540 --> 09:28.380
And then we pass SQL light its connection string.

09:28.380 --> 09:30.540
Now we're going to get this from configuration.

09:30.750 --> 09:36.000
And how we access our configuration is we specify builder dot configuration.

09:36.000 --> 09:42.120
And there's a special configuration value inside here called get connection string.

09:42.390 --> 09:44.250
And inside get connection string.

09:44.250 --> 09:47.010
We're going to specify default connection.

09:47.430 --> 09:50.580
And that's what we're going to create soon.

09:50.760 --> 09:55.860
So our code knows where to find our database server.

09:56.130 --> 10:00.570
And we're going to configure that and create a new migration next.
