Adding Entity Framework
In this lesson, we'll be adding Entity Framework to our project
Adding Entity Framework to our project#
In the last lesson, we discussed what Entity Framework is and the advantages of using it. To download it to our project, we will use the Nuget Gallery Extension that we installed in the Introduction module. If you haven't installed it yet, go to extension, search for Nuget Gallery and install it. Once it's installed, press cmd shift p if you're on mac or ctrl shift p on Windows; once the search box is open, search for
Nuget 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.EntityFrameworkCore.Sqlite, since this is going to be our 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're far off, you might get into some issues. Now press install, and you would 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
StoreContext.cs. Let's derive from DbContext class which is provided by EntityFrameworkCore. If we hover over it, it says
A DbContext instance represents a session with the database and can be used to query and save instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns. We discussed that it creates a session with the database. 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 that typically, you create a class that derives from DbContext and contains DbSet of type TEntity properties for each entity in the model. If the DbSet TEntity properties have a public setter, they are automatically initialized when the instance of the derived context is created. We created our module 1 lessons prior, and as you can see, we have public setters, so this means that when we create a new instance of our context, they will be automatically initialized.
Now coming to our StoreContext class, put your cursor inside DbContext; let's create a constructor. Let's choose the one which has options since we want to use the methods inside the DbContext class. Let's take off the Not Null Attribute since we're not going to use it in any way. But what we need is the DB context options and we pass in the options and then we use the base, which is the constructor inside the DB context class we're deriving from, and then pass through the options to that class.
Now below that, let's create our first database table which will contain our course properties as columns of the table. To create a database table, write public followed by Dbset. Our DbSet 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. 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 DbSet like this.