Adding Entity Framework
In this lesson, we'll be adding Entity Framework to our project
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 01:59] In the last lecture, we discussed what entity framework is and what are the advantages of using it. To download it to our project, we will use the nougat gallery extension that we installed in the introduction module. If you haven't installed it yet, go to extension, search for nougat gallery and install it. Once it's installed, press command shift np if you run Mac or control shift np if you're on windows. Now once the search box is open, search for nougat gallery and select it. It will take you to a different tab where you search for the required package and install it . We are looking for entity framework core. So search Microsoft dot entity framework core dot SQL light. Since this is going to be a development database, choose this package and select infrastructure. Since this is where we'll create our data context. Make sure the version you're selecting is same as the runtime. If not the same, make sure it's the closest one. If they are far off, you might get into some issues. Now press install and you see it's installing the package. Make sure there are no errors. Now close the tab, open the infrastructure project. Let's create a file and name it to context. Well, you don't have to mention dot CS, it does that automatically. Once the file is made, let's derive from DB context class, which is provided by entity framework core. If we hover over it, it says a DB context instance represents a session with a database and can be used to query and save instances of your entities. DB context is a combination of the unit of work and repository patents.
[02:00 - 03:41] We discussed that it creates a session with the database. And don't worry about the repository pattern and unit of work for now. We will be using these two as part of our architecture. It also says typically you create a class that derives from DB context and contains DB set of type T entity properties for each entity in the model. If the DB set of type T entity properties have a public setter, they are automatically initialized when the instance of the derived context is created. We created our models to lectures prior. And as you can see, we have public setters. So this means when we create a new instance of a context, there will be automatically initialized. Now coming to a store context class, put your cursor inside DB context. Let's create a constructor. Let's choose the one which has options since we want to use the methods inside the DB context class. Now let's take off the not null attribute since we are not going to use it anyway. But what we need is the DB context options. And then we pass in the options and then we use the base, which is the constructor inside the DB context class we are deriving from and then pass through the options to the class. Now below that, let's create a first database table, which will contain our course properties as columns of the table. To create a database table, right, public followed by a DB set. Our DB set will be of type course, since we want to map the course properties to the table, we'll give it a name courses, since this is going to be the name of the table.
[03:42 - 04:08] Finally, we end it with a getter and a setter. This is how you configure a database table. So whenever you need to create a new table, create a DB set just like this. The approach we are taking here is the code first approach, where we just write the properties of the model and our ORM maps it automatically to the database. This approach is convention based.
[04:09 - 05:21] What I mean here is if we take a look at a course model, we have named a unique property ID because this is a convention. And because it's named ID, entity framework or knows that it's unique and it will be a primary key of the table. And primary key of the table is non-nullable. So this is an example of a convention. If you name it something else, it will be difficult for entity framework or to select a primary key of the table. And if you know SQL, you know that every table must have a primary key. Now moving on, you remember the configure services method in the startup class? We need to register the database service in our startup class so that it's available from any project. Let's go back to our startup class, go inside the configure services method. This is where we need to register our DB context entity framework gives a method to add a database. So let's type services dot add TV context. This is going to be of type store context. That is the class we created in the infrastructure project. Inside, it will take some parameters.
[05:22 - 05:58] In our case, since we are using SQLite right now, we need to add that parameter. It gives us an error, which means we need to import it from entity framework. We also need to bring infrastructure so that this error is also removed. Now our SQLite connection requires a connection string. We are going to get a connection string from I configuration. Since we are here , I would like to mention something. Everyone has a preference and in C sharp world, there is a convention to use. Let's remove I configuration and also this from inside the constructor.
[05:59 - 06:36] Let's make configuration short to just config. Now put your cursor on it and use quick fix using command and dot or control and dot if you are on Windows and select initialize field from parameter. It will create private read only field called config. It will map the same property inside the constructor. It's possible that you didn't get an underscore config file. That's because I have chosen it to be this way through settings. If you want it to be the same way, go to settings search for private click on C sharp extensions.
[06:37 - 06:54] As you can see inside the private member prefix, I have selected an underscore. This is the prefix for generated private member declarations. This is not necessarily to use it the same way as I do, but this is a convention to use underscore as a prefix to private fields.
[06:55 - 08:12] You would have also noticed that my constructor is not using this keyword. This is something that I've tweaked as well. To take that off, search for this and inside the extensions, select C sharp extensions. You will see an option with says whether or not a constructor assignment of a property or variable should be perfect with this. Unselect this option. That's it. We will use this underscore config to get a SQLite connection string . Now let's go in, underscore config dot get connection string and name it default connection. Now let's add our connection string since we haven't added that yet. Let's go to the app settings dot development dot JSON file. Below our logging part, let's make our connection strings since this is where our eye configuration is going to look for. Since we have mentioned the key default connection, let's mention that here. Since we are using SQLite, we just need to mention data source. Let's name our database, learnify dot TB. Make sure the spellings are correct before you move on. Before we see the database in our project, let's create migration in the next lecture.
[08:13 - 08:28] [ Honors ]