EF Core is a popular Object-Relational Mapper (ORM) for retrieving and updating data. ASP.NET Core has good support for it, as you can imagine, as both are Microsoft tools. This section will present some common uses of EF Core with ASP.NET Core.
Make sure you firstinstall the latestdotnet-efglobal tool:
dotnet tool install --global dotnet-ef
Next, let's look at how to register contexts.
Registering DbContext
First, we can register a DbContext instance to the dependency injection framework:
services.AddDbContext<MyDbContext>(options =>
{
options.UseSqlServer(this.Configuration.GetConnectionString
("<connection string name>"));
});
As you can see in this example, we have to set the provider and its connection string; otherwise, the context is pretty much useless. By default, the context will be registered as a scoped instance, which is generally what we want...