Okay, now it's time to take a look at using Entity Framework.
Now that we have discussed it, and the first thing we're going to do is install the package that we
need to make use of this.
So we're going to go to our extensions.
One of them that we installed earlier on that you should have is NuGet Gallery.
So please do make sure you have this extension installed and available.
And the way that we use NuGet Gallery.
If you open up your terminal and let's just clear that window, then what you'll find is you've got
a tab here for NuGet.
And this is how we go ahead and install NuGet packages into our project.
What we want to search for here is Microsoft Dot Entity Framework Core.
And it should go ahead and search similarly named packages.
And lest we've made a mistake and I've spelt Microsoft wrong.
So that should be Microsoft Entity Framework Core.
And this should bring up a list of packages related to that.
Now we actually need two packages from this we we need Microsoft Entity Framework core dot SQLite.
Please install the package with the same name.
Don't install a similarly named one such as SQLite core.
That's not going to work for you.
You're going to get some strange errors when we start to use this.
So please install Microsoft Entity Framework Core SQLite.
Please pick the version that matches the major version of the runtime.
In my case, it's going to be version nine of this package.
If you're on dotnet nine, then just pick the the one that relates to what you're using.
Of course, if you're on dotnet ten you would select version ten of this.
It should be updated in line with dotnet as well.
So let's go ahead and add this by using the plus button and sending it to or installing it into API
Csproj.
And what we also need is Microsoft Entity Framework Core Design, which we need to install so that we
can create migrations with Entity Framework.
So we're also going to install this one into API.
Csproj as well.
And once those have been installed, then what you should be able to do is go to Solution Explorer.
Click on API and you should see the newly added packages Microsoft Entity Framework, Core Design and
Microsoft Entity Framework Core SQL Lite.
So please double check you have these installed.
Now that we have this, we can make use of Entity Framework classes inside our project.
And we're going to go to Solution Explorer inside the API.
Let's just right click and say New Folder.
And we're going to call this one data for all data related code.
And we're going to create a new file inside here.
And we're going to call it store context and not a new file.
We need to create a new class called Store Context and press return.
Now this class is going to derive from an entity framework class called Dbcontext.
So the way that we derive from another class is we add a colon after the name of the class, and we're
going to say dbcontext.
And this should bring in the available options.
And we want Dbcontext that's coming from Microsoft Entity Framework Core.
So I'll press return to select that and we get the using statement added at the top so that this class,
this Dbcontext class is coming from list namespace.
That's fine.
Now in order to use the Dbcontext and provide it with things like the connection string to our database
or our database server, we need to provide this with a constructor.
So a constructor in C-sharp world is when we create a new instance of the store context, then it runs
the code inside the constructor.
And the way that we create a constructor is we say Ctor to get the snippet for a constructor, and then
we tell it what parameters, if any, that it needs, that we need to provide.
When we create a new instance of this store context and the parameters that we do need to provide for
this.
We need to provide this with some DB context options and say options.
But there's a better way of creating a constructor.
Now in C-sharp eight and above.
And we get these ellipses underneath the store context.
And it tells us that we can use a primary constructor.
So if we go ahead and use this, and what we have available to us is a quick fix, when we see this
light bulb or we see ellipses or we see an underline in our code, we can hover over this and we can
either select the quick fix here, we can use the light bulb here, or we can use the shortcut key combination,
which in my case is command and period.
And this will open up a menu.
And it will give us an option of what to use.
And we can use the primary constructor.
And then we pass in the options.
Now these options have to be passed to the class that we're deriving from.
So as well as Dbcontext here, we need to add parentheses and specify options so that these options,
which is going to contain our connection string, gets passed up to the Entity Framework Dbcontext.
Now if we hover over the Dbcontext then this gives us documentation about the Dbcontext.
Although because I've specified options, let's just remove that for the time being.
Hover over the Dbcontext just to get a description about what this class actually does.
Now this represents a session with the database and can be used to query and save instances of our entities.
So when it comes to saving a product into the database or querying for a product in the database, then
we use it via our store context which derives from the Dbcontext.
Now, if you've done some coding before, you may well have heard of the unit of work and repository
patterns.
The Dbcontext is a combination of these patterns.
Now we can also use them inside our project if we want to, or because this is a combination of those
two patterns, we don't actually have to use them in our project.
Now, I have covered those patterns on different courses, so I'm not going to cover that on this one.
I'm going to keep things nice and simple.
And because the Dbcontext is already a combination of these two patterns, we don't have to use it in
our project.
So that's the dbcontext.
And we also need to specify.
Well let's put the options back in.
Now I've just got the description from that.
We also need to specify inside here our different entities.
And we do that we create a property.
And this is going to be a type of dbset.
And if we hover over the dbset then this is or can be used to query and save instances of our entity.
And Linq queries against a DB set will be translated into queries against the database.
So what this means is when we use Entity Framework is we write our code in C-sharp, which SQL doesn't
understand, and then Entity Framework translates those C-sharp or that C-sharp code into SQL commands
that our database does understand.
So we only need to use one language.
We don't need to know C-sharp and SQL syntax.
We just need to know C-sharp to be able to query our database.
Now our DB set takes a type.
So we put the type of thing in angle brackets.
And this is going to be a type of product.
So I'll press return.
And then we give our property a name.
And I'm going to call it products.
And because we're getting the warning here because of the type of property, this is being a DB set.
It says must contain a non-null value or consider adding a required modifier.
This actually caught me by surprise.
This must have been recently added to Entity Framework version nine or dotnet nine, because we didn't
used to have to add the required modifier here.
So I'm going to see how this goes.
It maybe it will cause us a problem.
Maybe it will not.
I was not expecting to need to use that inside this particular class anyway.
Now that we have this available in here, we need to tell our startup code about this.
Now our startup code is contained inside the Program.cs, and our Dbcontext class is something that
we're going to want to use in other parts of our application so that we can query our database.
So for that reason, we add this as a service inside our program class.
And we're going to specify builder dot services.
And there's a special method inside here to add our dbcontext.
Inside the angle brackets we specify store context.
And then we provide it with the options.
And the options are what we specify here.
So we need to pass this to our store context.
Because when we add this as a service the framework will create a new instance of our store context.
So it needs to know about the options we're going to provide to it.
So back to our program class we're going to specify.
Opt.
And then we provide this as an expression.
So we effectively add an arrow with the equals and angle brackets.
And then before the closing parentheses we'll add curly brackets.
And we close off the statement with a semicolon.
And then for the options we can say options and use SQL Lite.
And then we pass SQL light its connection string.
Now we're going to get this from configuration.
And how we access our configuration is we specify builder dot configuration.
And there's a special configuration value inside here called get connection string.
And inside get connection string.
We're going to specify default connection.
And that's what we're going to create soon.
So our code knows where to find our database server.
And we're going to configure that and create a new migration next.