In this recipe, we will learn a simple way of registering, resolving, and giving a life cycle to EF in ASP.NET Core.
We will inject EF in the ConfigureServices
method of Statup.cs
. The IServiceCollection
interface parameter of ConfigureServices
already has some helpers for the most common libraries used in ASP.NET MVC.
This code does not exist by default in the empty project in Visual Studio's ASP.NET Core templates, but it already exists if we use a Web Application ASP.NET Core template.
After that, we will add a class that inherits from DbContext
to our application to perform database operations that throw an ORM. Finally, we will create a repository layer between the application and the database persistence layer (even if all the code is physically in the same application) before configuring its life cycle.
But, first of all, let's create a database on SQL Server (from 2008 to 2014) named CookBook
.
After that, we will create...