Skip to content

Commit b10b4f3

Browse files
committed
work
1 parent 643ef5a commit b10b4f3

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

entity-framework/core/dbcontext-configuration/index.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ uid: core/dbcontext-configuration/index
1010

1111
This article shows basic patterns for initialization and configuration of a <xref:Microsoft.EntityFrameworkCore.DbContext> instance.
1212

13+
[!INCLUDE [managed-identities-test-non-production](~/core/includes/managed-identities-test-non-production.md)]
14+
1315
## The DbContext lifetime
1416

1517
The lifetime of a `DbContext` begins when the instance is created and ends when the instance is [disposed](/dotnet/standard/garbage-collection/unmanaged). A `DbContext` instance is designed to be used for a _single_ [unit-of-work](https://www.martinfowler.com/eaaCatalog/unitOfWork.html). This means that the lifetime of a `DbContext` instance is usually very short.
@@ -29,8 +31,10 @@ A typical unit-of-work when using Entity Framework Core (EF Core) involves:
2931

3032
> [!IMPORTANT]
3133
>
32-
> - It is important to dispose the <xref:Microsoft.EntityFrameworkCore.DbContext> after use. This ensures both that any unmanaged resources are freed, and that any events or other hooks are unregistered so as to prevent memory leaks in case the instance remains referenced.
33-
> - [DbContext is **not thread-safe**](#avoiding-dbcontext-threading-issues). Do not share contexts between threads. Make sure to [await](/dotnet/csharp/language-reference/operators/await) all async calls before continuing to use the context instance.
34+
> - It is important to dispose the <xref:Microsoft.EntityFrameworkCore.DbContext> after use. This ensures any:
35+
> - Unmanaged resources are freed.
36+
> - Events or other hooks are unregistered. Unregistering prevents memory leaks when the instance remains referenced.
37+
> - [DbContext is **Not thread-safe**](#avoiding-dbcontext-threading-issues). Don't share contexts between threads. Make sure to [await](/dotnet/csharp/language-reference/operators/await) all async calls before continuing to use the context instance.
3438
> - An <xref:System.InvalidOperationException> thrown by EF Core code can put the context into an unrecoverable state. Such exceptions indicate a program error and are not designed to be recovered from.
3539
3640
## DbContext in dependency injection for ASP.NET Core
@@ -86,16 +90,15 @@ The `ApplicationDbContext` class must expose a public constructor with a `DbCont
8690
-->
8791
[!code-csharp[MyController](../../../samples/core/Miscellaneous/ConfiguringDbContext/WebApp/Controllers/MyController.cs?name=MyController)]
8892

89-
9093
The final result is an `ApplicationDbContext` instance created for each request and passed to the controller to perform a unit-of-work before being disposed when the request ends.
9194

92-
Read further in this article to learn more about configuration options. In addition, see [App startup in ASP.NET Core](/aspnet/core/fundamentals/startup) and [Dependency injection in ASP.NET Core](/aspnet/core/fundamentals/dependency-injection) for more information on configuration and dependency injection in ASP.NET Core.
95+
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.
9396

9497
<!-- See also [Using Dependency Injection](TODO) for advanced dependency injection configuration with EF Core. -->
9598

96-
## Simple DbContext initialization with 'new'
99+
## Basic DbContext initialization with 'new'
97100

98-
`DbContext` instances can be constructed in the normal .NET way, for example with `new` in C#. Configuration can be performed by overriding the `OnConfiguring` method, or by passing options to the constructor. For example:
101+
`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:
99102

100103
<!--
101104
public class ApplicationDbContext : DbContext
@@ -152,7 +155,7 @@ The `DbContextOptions` can be created and the constructor can be called explicit
152155
-->
153156
[!code-csharp[UseNewForWebApp](../../../samples/core/Miscellaneous/ConfiguringDbContext/WebApp/UseNewForWebApp.cs?name=UseNewForWebApp)]
154157

155-
## Using a DbContext factory (e.g. for Blazor)
158+
## Using a DbContext factory, e.g. for Blazor
156159

157160
Some application types (e.g. [ASP.NET Core Blazor](/aspnet/core/blazor/)) use dependency injection but do not create a service scope that aligns with the desired `DbContext` lifetime. Even where such an alignment does exist, the application may need to perform multiple units-of-work within this scope. For example, multiple units-of-work within a single HTTP request.
158161

samples/core/Miscellaneous/ConfiguringDbContext/WebApp9/Data/ApplicationDbContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
public class ApplicationDbContext : DbContext
44
{
5-
public ApplicationDbContext(
6-
DbContextOptions<ApplicationDbContext> options)
5+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
76
: base(options)
87
{
98
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
public class ApplicationDbContext2 : DbContext
4+
{
5+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
6+
{
7+
optionsBuilder.UseSqlServer(
8+
@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
9+
}
10+
}

samples/core/Miscellaneous/ConfiguringDbContext/WebApp9/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
// <snippet_1>
77
var connectionString =
88
builder.Configuration.GetConnectionString("DefaultConnection")
9-
?? throw new InvalidOperationException("Connection string"
10-
+ "'DefaultConnection' not found.");
9+
?? throw new InvalidOperationException("Connection string"
10+
+ "'DefaultConnection' not found.");
11+
1112
builder.Services.AddDbContext<ApplicationDbContext>(options =>
1213
options.UseSqlServer(connectionString));
1314
// </snippet_1>

samples/core/Miscellaneous/ConfiguringDbContext/WithContextFactory/FactoryServicesExample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public class FactoryServicesExample
1010
public void ConfigureServices(IServiceCollection services)
1111
{
1212
services.AddDbContextFactory<ApplicationDbContext>(
13-
options =>
14-
options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0"));
13+
options => options.UseSqlServer(
14+
@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0"));
1515
}
1616
#endregion
1717
}

samples/core/Miscellaneous/ConfiguringDbContext/WithNew/ApplicationDbContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public class ApplicationDbContext : DbContext
77
{
88
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
99
{
10-
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
10+
optionsBuilder.UseSqlServer(
11+
@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
1112
}
1213
}
1314
#endregion

0 commit comments

Comments
 (0)