Configuring the database connection with DbContextOptions<TContext>

When DbContext is created, it needs to be configured so that it knows <indexentry content="DbContext:database connection configuration with DbContextOptions"> which database to connect to, and with which database provider. The recommended way for passing the connection information to DbContext is by using the DbContextOptions<TContext> type (where TContext is the DbContext type that the options apply to).

You can create DbContextOptions inside the OnConfiguring method of your DbContext class itself, but this approach is more difficult to maintain. Instead, I recommend setting DbContextOptions in your application startup and injecting it into your DbContext class.

ASP.NET Core makes heavy use of Dependency Injection (more information is available at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection). In this technique, you register the types and instance that your application uses inside an object called a Dependency Injection Container. When resolving instances from the container, it will take care of injecting the dependencies needed by the class you asked to resolve.

These are the steps needed to work with DbContextOptions:

  1. Add a constructor to your DbContext class that accepts the DbContextOptions instance and passes it to its base class:
public GiveNTakeContext(DbContextOptions<GiveNTakeContext> options)
: base(options)
{ }
  1. Register the DbContext class in the ConfigureServices method of your startup class and pass it the necessary connection string:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<GiveNTakeContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("GiveNTakeDB")));
}

  1. Add the connection string to your application settings file (appsettings.json). If the file doesn't exist in your project, right-click the project, click Add New Item, and select ASP.NET Configuration File:

  1. The newly added file should include a default connection string value. Change the database name to a meaningful name, such as GiveNTake:

The registration code you inserted in your <indexentry content="DbContext:database connection configuration with DbContextOptions"> startup class not only set the DbContextOptions instance; it also registered the DbContext type to the application's Dependency Injection Container. Now, if you add a DbContext (such as GiveNTakeContext) parameter to your controller's constructor, ASP.NET Core will inject its instance to the controller, when it is created, by a request that is routed to it:

[Route("api/[controller]")]
public class ProductsController : Controller
{
public ProductsController(GiveNTakeContext context)
{
_context = context;
}
...
}

However, if you try to run the application now and navigate to an action in ProductsController, you will get an exception. This is because we haven't told EF Core to create the database for us. To create and manage the database from your code, you need to use EF Core Migrations. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.124