dotnet / dotnet/EntityFramework.Docs
DbContextOptions as singleton
- Dominant language
- Mermaid
- Stars
- 1.7k
- Forks
- 2k
- Avg merge
- 7d 23h
- Merged PRs (30d)
- 16
Description
Please provide some guidance regarding the scope in which DbContextOptions should be configured. Eg., is this safe:
```cs
public static IServiceCollection AddMyAppDbContextOptions(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton>(serviceProvider =>
{
var loggerFactory = serviceProvider.GetRequiredService();
var options = serviceProvider.GetRequiredService>();
var dbContextOptionsBuilder = new DbContextOptionsBuilder()
.UseSqlServer(options.Value.DatabaseConnection, b => b.MigrationsAssembly(MyAppConstants.DatabaseMigrationsAssembly))
.UseLoggerFactory(loggerFactory);
if (options.Value.DatabaseEnableSensitiveDataLogging) dbContextOptionsBuilder.EnableSensitiveDataLogging();
return dbContextOptionsBuilder.Options;
});
return serviceCollection;
}
```
---
#### 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: [Configuring a DbContext - 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
Assessment
This issue has not been assessed yet.