dotnet / dotnet/EntityFramework.Docs

DbContext Lifetime, Configuration doesn't align with Options pattern

Open
#3,098 0 comments 0 reactions 0 assignees View on GitHub
area-context-management
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

The page doesn't align with another MS docs page, describing the ASP.NET Core [Options pattern](https://docs.microsoft.com/aspnet/core/fundamentals/dependency-injection#ASP0000).

The hard-coded examples on the page are informative, yet don't reflect real business situation where connection string is provided by ASP.NET (Core) configuration.

I'm missing a description in either documentation on how to use the [Options pattern](https://docs.microsoft.com/aspnet/core/fundamentals/dependency-injection#ASP0000) with [`AddDbContext`](https://docs.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.entityframeworkservicecollectionextensions.adddbcontext).

\ref: dotnet/AspNetCore.Docs/issues/21441

---

To give more detail:

```c#
services.Configure(_config)
.AddDbContext(options => options.UseSqlServer(_config.Get().ConnectionStrings?.SqlServer, providerOptions => providerOptions.EnableRetryOnFailure()));
```

... works, yet dependency injection should be used for configuration:
```c#
services.Configure(_config)
.AddDbContext((options, config) => options.UseSqlServer(config.ConnectionStrings?.SqlServer, providerOptions => providerOptions.EnableRetryOnFailure()));
```

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: 055bb8db-139d-beb5-61c1-47cf1ea6ea93
* Version Independent ID: b0a3d5ad-b62c-1ff4-386a-f11e350eea0c
* Content: [DbContext Lifetime, Configuration, and Initialization - EF Core](https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/)
* Content Source: [entity-framework/core/dbcontext-configuration/index.md](https://github.com/dotnet/EntityFramework.Docs/blob/master/entity-framework/core/dbcontext-configuration/index.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @ajcvickers
* Microsoft Alias: **avickers**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.