You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: entity-framework/core/dbcontext-configuration/index.md
+74-2Lines changed: 74 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,8 +90,6 @@ The final result is an `ApplicationDbContext` instance created for each request
90
90
91
91
Read further in this article to learn more about configuration options. See [Dependency injection in ASP.NET Core](/aspnet/core/fundamentals/dependency-injection) for more information.
92
92
93
-
<!-- See also [Using Dependency Injection](TODO) for advanced dependency injection configuration with EF Core. -->
94
-
95
93
## Basic DbContext initialization with 'new'
96
94
97
95
`DbContext` instances can be constructed with `new` in C#. Configuration can be performed by overriding the `OnConfiguring` method, or by passing options to the constructor. For example:
@@ -425,6 +423,80 @@ Any code that explicitly executes multiple threads in parallel should ensure tha
425
423
426
424
Using dependency injection, this can be achieved by either registering the context as scoped, and creating scopes (using `IServiceScopeFactory`) for each thread, or by registering the `DbContext` as transient (using the overload of `AddDbContext` which takes a `ServiceLifetime` parameter).
427
425
426
+
<aname="configuredbcontext"></a>
427
+
428
+
## ConfigureDbContext for configuration composition
429
+
430
+
> [!NOTE]
431
+
> This section covers intermediate-level usage of EF Core primarily intended for reusable libraries and components. Most applications should use the `AddDbContextFactory` pattern described earlier in this article.
432
+
433
+
Starting with EF Core 9.0, you can use <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.ConfigureDbContext*> to apply additional configuration to a `DbContext` either before or after the `AddDbContext` call. This is particularly useful for composing non-conflicting configuration in reusable components or tests.
434
+
435
+
### Basic ConfigureDbContext usage
436
+
437
+
`ConfigureDbContext` allows you to add configuration in a reusable library or component without replacing the entire provider configuration:
### Provider-specific configuration without connection strings
454
+
455
+
To apply provider-specific configuration you can use provider-specific configuration methods without supplying the connection string. The SQL Server provider also includes `ConfigureSqlEngine` for this case. See [SQL Server-specific batching behavior](xref:core/providers/sql-server/misc#configuresqlengine) for more information.
### ConfigureDbContext and AddDbContext precedence
472
+
473
+
When both `ConfigureDbContext` and `AddDbContext` are used, or when multiple calls to these methods are made, the configuration is applied in the order the methods are called, with later calls taking precedence for conflicting options.
474
+
475
+
For non-conflicting options (like adding logging, interceptors, or other settings), all configurations are composed together:
For conflicting options, the last configuration wins. See [breaking changes in EF Core 8.0](xref:core/what-is-new/ef-core-8.0/breaking-changes#AddDbContext) for more information about this behavior change.
494
+
495
+
> [!NOTE]
496
+
> Configuring a different provider will not remove the previous provider configuration. This can lead to errors when creating the context. To completely replace the provider, you need to remove the context registration and re-add it, or create a new service collection.
497
+
498
+
<!-- See also [Using Dependency Injection](TODO) for advanced dependency injection configuration with EF Core. -->
499
+
428
500
## More reading
429
501
430
502
- Read [Dependency Injection](/aspnet/core/fundamentals/dependency-injection) to learn more about using DI.
Thiswaschangedtobeconsistentwiththenewmethod `ConfigureDbContext`,thatenablesconfigurationcomposabilityfornon-conflictingconfigurations. See [DbContextconfiguration](xref:core/dbcontext-configuration/index#configuredbcontext) formoreinformation.
0 commit comments