Skip to content

Commit 9475a4e

Browse files
Removed all uses of after feedback from @AndriySvyryd.
1 parent 7d8b758 commit 9475a4e

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

docs/database/use-entity-framework-db-contexts.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ The most commonly used .NET Aspire EF client integrations are:
2323
> [!NOTE]
2424
> In .NET Aspire, EF is implemented by client integrations, not hosting integrations. So, for example, to use EF with a SQL Server database, you'd use the SQL Server hosting integration to create the SQL Server container and add a database to it. In the consuming microservices, when you want to use EF, choose the SQL Server Entity Framework Core integration instead of the SQL Server client integration.
2525
26-
## Use .NET Aspire to create a database context
26+
## Use .NET Aspire to create an EF context
2727

2828
In EF, a [**context**](/ef/core/dbcontext-configuration/) is a class used to interact with the database. Contexts inherit from the <xref:Microsoft.EntityFrameworkCore.DbContext> class. They provide access to the database through properties of type `DbSet<T>`, where each `DbSet` represents a table or collection of entities in the database. The context also manages database connections, tracks changes to entities, and handles operations like saving data and executing queries.
2929

3030
The .NET Aspire EF client integrations each include an extension method named `Add\<DatabaseSystem\>DbContext`, where **\<DatabaseSystem\>** is a string identifying the database product you are using. For example, for the SQL Server EF client integration, the method is named <xref:Microsoft.Extensions.Hosting.AspireSqlServerEFCoreSqlClientExtensions.AddSqlServerDbContext%2A> and for the PostgreSQL client integration, the method is named <xref:Microsoft.Extensions.Hosting.AspireEFPostgreSqlExtensions.AddNpgsqlDbContext%2A>.
3131

3232
These .NET Aspire add context methods:
3333

34-
- Check that a database context of the same type is not already registered in the Dependency Injection (DI) container.
34+
- Check that a context of the same type is not already registered in the Dependency Injection (DI) container.
3535
- Use the connection name you pass to the method to get the connection string from the application builder. This connection name must match the name used when adding the corresponding resource to the app host project.
3636
- Apply the `DbContext` options, if you passed them.
3737
- Add the specified `DbContext` to the DI container with context pooling enabled.
@@ -40,7 +40,7 @@ These .NET Aspire add context methods:
4040
- Enable health checks.
4141
- Enable connection resiliency.
4242

43-
Use these .NET Aspire add context methods when you want a simple way to create the database context and don't yet need advanced EF customization.
43+
Use these .NET Aspire add context methods when you want a simple way to create a context and don't yet need advanced EF customization.
4444

4545
:::zone pivot="sql-server-ef"
4646

@@ -133,7 +133,7 @@ builder.Services.AddDbContextPool<ExampleDbContext>(options =>
133133

134134
:::zone-end
135135

136-
You have more flexibility when you create the database context in this way, for example:
136+
You have more flexibility when you create the context in this way, for example:
137137

138138
- You can reuse existing configuration code for the context without rewriting it for .NET Aspire.
139139
- You can choose not to use EF Core context pooling, which may be necessary in some circumstances. For more information, see [Use EF context pooling in .NET Aspire](#use-ef-context-pooling-in-net-aspire)
@@ -202,7 +202,7 @@ builder.EnrichMySqlDbContext<ExampleDbContext>(
202202

203203
:::zone-end
204204

205-
Retrieve the database context from the DI container using the same code as the previous example:
205+
Obtain the context from the DI container using the same code as the previous example:
206206

207207
```csharp
208208
public class ExampleService(ExampleDbContext context)
@@ -300,7 +300,7 @@ Most microservices always connect to the same database with the same credentials
300300
- You might offer your service to multiple tenants and need to use a different database depending on which customer made the request.
301301
- You might need to authenticate the request with a different database user account depending on which customer made the request.
302302

303-
For these requirements, you can use code to formulate a **dynamic connection string** and then use it to reach the database and run queries. However, this technique isn't supported by the .NET Aspire `Add\<DatabaseSystem\>DbContext` methods. Instead you must use the EF method to create the database context and then enrich it:
303+
For these requirements, you can use code to formulate a **dynamic connection string** and then use it to reach the database and run queries. However, this technique isn't supported by the .NET Aspire `Add\<DatabaseSystem\>DbContext` methods. Instead you must use the EF method to create the context and then enrich it:
304304

305305
:::zone pivot="sql-server-ef"
306306

@@ -387,15 +387,15 @@ builder.EnrichMySqlDbContext<ExampleDbContext>(
387387

388388
:::zone-end
389389

390-
The above code replaces the place holder `{DatabaseName}` in the connection string with the string `ContosoDatabase`, at run time, before it creates the database context and enriches it.
390+
The above code replaces the place holder `{DatabaseName}` in the connection string with the string `ContosoDatabase`, at run time, before it creates the context and enriches it.
391391

392392
### Use EF Core context factories in .NET Aspire
393393

394394
An EF context is an object designed to be used for a single unit of work. For example, if you want to add a new customer to the database, you might need to add a row in the **Customers** table and a row in the **Addresses** table. You should get the EF context, add the new customer and address entities to it, call <xref:Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync*>, and then dispose the context.
395395

396-
In many types of web application, such as ASP.NET applications, each HTTP request closely corresponds to a single unit of work against the database. If your .NET Aspire microservice is an ASP.NET application or a similar web application, you can use the standard EF <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContextPool*> method described above to register a context that is tied to the current HTTP request. Remember to call the .NET Aspire `Enrich\<DatabaseSystem\>DbContext` method to gain health checks, tracing, and other features. When you use this approach, the database context lifetime is tied to the web request. You don't have to call the <xref:Microsoft.EntityFrameworkCore.DbContext.Dispose*> method when the unit of work is complete.
396+
In many types of web application, such as ASP.NET applications, each HTTP request closely corresponds to a single unit of work against the database. If your .NET Aspire microservice is an ASP.NET application or a similar web application, you can use the standard EF <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContextPool*> method described above to register a context that is tied to the current HTTP request. Remember to call the .NET Aspire `Enrich\<DatabaseSystem\>DbContext` method to gain health checks, tracing, and other features. When you use this approach, the context lifetime is tied to the web request. You don't have to call the <xref:Microsoft.EntityFrameworkCore.DbContext.Dispose*> method when the unit of work is complete.
397397

398-
Other application types, such as ASP.NET Core Blazor, don't necessarily align each request with a unit of work, because they use dependency injection with a different service scope. In such apps, you may need to perform multiple units of work, each with a different database context, within a single HTTP request and response. To implement this approach, you can register a context factory, by calling the EF <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddPooledDbContextFactory*> method. This method also partners well with the .NET Aspire `Enrich\<DatabaseSystem\>DbContext` methods:
398+
Other application types, such as ASP.NET Core Blazor, don't necessarily align each request with a unit of work, because they use dependency injection with a different service scope. In such apps, you may need to perform multiple units of work, each with a different context, within a single HTTP request and response. To implement this approach, you can register a context factory, by calling the EF <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddPooledDbContextFactory*> method. This method also partners well with the .NET Aspire `Enrich\<DatabaseSystem\>DbContext` methods:
399399

400400
:::zone pivot="sql-server-ef"
401401

@@ -417,7 +417,7 @@ builder.EnrichSqlServerDbContext<ExampleDbContext>(
417417

418418
```csharp
419419
builder.Services.AddPooledDbContextFactory<ExampleDbContext>(options =>
420-
options.UseNpgsql(connectionString
420+
options.UseNpgsql(builder.Configuration.GetConnectionString("database")
421421
?? throw new InvalidOperationException("Connection string 'database' not found.")));
422422

423423
builder.EnrichNpgsqlDbContext<ExampleDbContext>(
@@ -433,7 +433,7 @@ builder.EnrichNpgsqlDbContext<ExampleDbContext>(
433433

434434
```csharp
435435
builder.Services.AddPooledDbContextFactory<ExampleDbContext>(options =>
436-
options.UseOracle(connectionString
436+
options.UseOracle(builder.Configuration.GetConnectionString("database")
437437
?? throw new InvalidOperationException("Connection string 'database' not found.")));
438438

439439
builder.EnrichOracleDatabaseDbContext<ExampleDbContext>(
@@ -449,7 +449,7 @@ builder.EnrichOracleDatabaseDbContext<ExampleDbContext>(
449449

450450
```csharp
451451
builder.Services.AddPooledDbContextFactory<ExampleDbContext>(options =>
452-
options.UseMySql(connectionString
452+
options.UseMySql(builder.Configuration.GetConnectionString("database")
453453
?? throw new InvalidOperationException("Connection string 'database' not found.")));
454454

455455
builder.EnrichMySqlDbContext<ExampleDbContext>(
@@ -478,13 +478,13 @@ Contexts created from factories in this way aren't disposed of automatically bec
478478

479479
## Use EF context pooling in .NET Aspire
480480

481-
In EF Core a database context is relatively quick to create and dispose of so most applications can set them up as needed without impacting their performance. However, the overhead is not zero so, if your microservice intensively creates contexts, you may observe suboptimal performance. In such situations, consider using a database context pool.
481+
In EF Core a context is relatively quick to create and dispose of so most applications can set them up as needed without impacting their performance. However, the overhead is not zero so, if your microservice intensively creates contexts, you may observe suboptimal performance. In such situations, consider using a context pool.
482482

483483
Context pooling is a feature of EF Core. Contexts are created as normal but, when you dispose of one, it isn't destroyed but reset and stored in a pool. The next time your code creates a context, the stored one is returned to avoid the extra overhead of creating a new one.
484484

485485
In a .NET Aspire consuming project, there are three ways to use context pooling:
486486

487-
- Use the .NET Aspire `Add\<DatabaseSystem\>DbContext` methods to create the database context. These methods create a context pool automatically.
487+
- Use the .NET Aspire `Add\<DatabaseSystem\>DbContext` methods to create the context. These methods create a context pool automatically.
488488
- Call the EF <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContextPool*> method instead of the EF <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext*> method.
489489

490490
:::zone pivot="sql-server-ef"
@@ -563,7 +563,7 @@ In a .NET Aspire consuming project, there are three ways to use context pooling:
563563

564564
:::zone-end
565565

566-
Remember to enrich the database context after using the last two methods, as described above.
566+
Remember to enrich the context after using the last two methods, as described above.
567567

568568
> [!IMPORTANT]
569569
> Only the base context state is reset when it's returned to the pool. If you've manually changed the state of the `DbConnection` or another service, you must also manually reset it. Additionally, context pooling prevents you from using `OnConfiguring` to configure the context. See [DbContext pooling](/ef/core/performance/advanced-performance-topics#dbcontext-pooling) for more information.

0 commit comments

Comments
 (0)