Skip to content

Commit 14a9317

Browse files
authored
Drop markdown headings to solve dup headings (#19847)
1 parent 3a893c1 commit 14a9317

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

aspnetcore/blazor/blazor-server-ef-core.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Blazor Server is a stateful app framework. The app maintains an ongoing connecti
2020
> [!NOTE]
2121
> This article addresses EF Core in Blazor Server apps. Blazor WebAssembly apps run in a WebAssembly sandbox that prevents most direct database connections. Running EF Core in Blazor WebAssembly is beyond the scope of this article.
2222
23-
## Sample app
23+
<h2 id="sample-app-5x">Sample app</h2>
2424

2525
The sample app was built as a reference for Blazor Server apps that use EF Core. The sample app includes a grid with sorting and filtering, delete, add, and update operations. The sample demonstrates use of EF Core to handle optimistic concurrency.
2626

@@ -35,7 +35,7 @@ The grid, add, and view components use the "context-per-operation" pattern, wher
3535
> [!NOTE]
3636
> Some of the code examples in this topic require namespaces and services that aren't shown. To inspect the fully working code, including the required [`@using`](xref:mvc/views/razor#using) and [`@inject`](xref:mvc/views/razor#inject) directives for Razor examples, see the [sample app](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/blazor/common/samples/5.x/BlazorServerEFCoreSample).
3737
38-
## Database access
38+
<h2 id="database-access-5x">Database access</h2>
3939

4040
EF Core relies on a <xref:Microsoft.EntityFrameworkCore.DbContext> as the means to [configure database access](/ef/core/miscellaneous/configuring-dbcontext) and act as a [*unit of work*](https://martinfowler.com/eaaCatalog/unitOfWork.html). EF Core provides the <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext%2A> extension for ASP.NET Core apps that registers the context as a *scoped* service by default. In Blazor Server apps, scoped service registrations can be problematic because the instance is shared across components within the user's circuit. <xref:Microsoft.EntityFrameworkCore.DbContext> isn't thread safe and isn't designed for concurrent use. The existing lifetimes are inappropriate for these reasons:
4141

@@ -75,9 +75,9 @@ The following recommendations are designed to provide a consistent approach to u
7575

7676
Place operations after the `Loading = true;` line in the `try` block.
7777

78-
* For longer-lived operations that take advantage of EF Core's [change tracking](/ef/core/querying/tracking) or [concurrency control](/ef/core/saving/concurrency), [scope the context to the lifetime of the component](#scope-to-the-component-lifetime).
78+
* For longer-lived operations that take advantage of EF Core's [change tracking](/ef/core/querying/tracking) or [concurrency control](/ef/core/saving/concurrency), [scope the context to the lifetime of the component](#scope-to-the-component-lifetime-5x).
7979

80-
### New DbContext instances
80+
<h3 id="new-dbcontext-instances-5x">New DbContext instances</h3>
8181

8282
The fastest way to create a new <xref:Microsoft.EntityFrameworkCore.DbContext> instance is by using `new` to create a new instance. However, there are several scenarios that may require resolving additional dependencies. For example, you may wish to use [`DbContextOptions`](/ef/core/miscellaneous/configuring-dbcontext#configuring-dbcontextoptions) to configure the context.
8383

@@ -94,7 +94,7 @@ The factory is injected into components and used to create new instances. For ex
9494
> [!NOTE]
9595
> `Wrapper` is a [component reference](xref:blazor/components/index#capture-references-to-components) to the `GridWrapper` component. See the `Index` component (`Pages/Index.razor`) in the [sample app](https://github.com/dotnet/AspNetCore.Docs/blob/master/aspnetcore/blazor/common/samples/5.x/BlazorServerEFCoreSample/BlazorServerDbContextExample/Pages/Index.razor).
9696
97-
### Scope to the component lifetime
97+
<h3 id="scope-to-the-component-lifetime-5x">Scope to the component lifetime</h3>
9898

9999
You may wish to create a <xref:Microsoft.EntityFrameworkCore.DbContext> that exists for the lifetime of a component. This allows you to use it as a [unit of work](https://martinfowler.com/eaaCatalog/unitOfWork.html) and take advantage of built-in features, such as change tracking and concurrency resolution.
100100
You can use the factory to create a context and track it for the lifetime of the component. First, implement <xref:System.IDisposable> and inject the factory as shown in `Pages/EditContact.razor`:
@@ -121,7 +121,7 @@ Blazor Server is a stateful app framework. The app maintains an ongoing connecti
121121
> [!NOTE]
122122
> This article addresses EF Core in Blazor Server apps. Blazor WebAssembly apps run in a WebAssembly sandbox that prevents most direct database connections. Running EF Core in Blazor WebAssembly is beyond the scope of this article.
123123
124-
## Sample app
124+
<h2 id="sample-app-3x">Sample app</h2>
125125

126126
The sample app was built as a reference for Blazor Server apps that use EF Core. The sample app includes a grid with sorting and filtering, delete, add, and update operations. The sample demonstrates use of EF Core to handle optimistic concurrency.
127127

@@ -136,16 +136,14 @@ The grid, add, and view components use the "context-per-operation" pattern, wher
136136
> [!NOTE]
137137
> Some of the code examples in this topic require namespaces and services that aren't shown. To inspect the fully working code, including the required [`@using`](xref:mvc/views/razor#using) and [`@inject`](xref:mvc/views/razor#inject) directives for Razor examples, see the [sample app](https://github.com/dotnet/AspNetCore.Docs/tree/master/aspnetcore/blazor/common/samples/3.x/BlazorServerEFCoreSample).
138138
139-
## Database access
139+
<h2 id="database-access-3x">Database access</h2>
140140

141141
EF Core relies on a <xref:Microsoft.EntityFrameworkCore.DbContext> as the means to [configure database access](/ef/core/miscellaneous/configuring-dbcontext) and act as a [*unit of work*](https://martinfowler.com/eaaCatalog/unitOfWork.html). EF Core provides the <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext%2A> extension for ASP.NET Core apps that registers the context as a *scoped* service by default. In Blazor Server apps, this can be problematic because the instance is shared across components within the user's circuit. <xref:Microsoft.EntityFrameworkCore.DbContext> isn't thread safe and isn't designed for concurrent use. The existing lifetimes are inappropriate for these reasons:
142142

143143
* **Singleton** shares state across all users of the app and leads to inappropriate concurrent use.
144144
* **Scoped** (the default) poses a similar issue between components for the same user.
145145
* **Transient** results in a new instance per request; but as components can be long-lived, this results in a longer-lived context than may be intended.
146146

147-
## Database access
148-
149147
The following recommendations are designed to provide a consistent approach to using EF Core in Blazor Server apps.
150148

151149
* By default, consider using one context per operation. The context is designed for fast, low overhead instantiation:
@@ -178,9 +176,9 @@ The following recommendations are designed to provide a consistent approach to u
178176

179177
Place operations after the `Loading = true;` line in the `try` block.
180178

181-
* For longer-lived operations that take advantage of EF Core's [change tracking](/ef/core/querying/tracking) or [concurrency control](/ef/core/saving/concurrency), [scope the context to the lifetime of the component](#scope-to-the-component-lifetime).
179+
* For longer-lived operations that take advantage of EF Core's [change tracking](/ef/core/querying/tracking) or [concurrency control](/ef/core/saving/concurrency), [scope the context to the lifetime of the component](#scope-to-the-component-lifetime-3x).
182180

183-
### Create new DbContext instances
181+
<h3 id="new-dbcontext-instances-3x">New DbContext instances</h3>
184182

185183
The fastest way to create a new <xref:Microsoft.EntityFrameworkCore.DbContext> instance is by using `new` to create a new instance. However, there are several scenarios that may require resolving additional dependencies. For example, you may wish to use [`DbContextOptions`](/ef/core/miscellaneous/configuring-dbcontext#configuring-dbcontextoptions) to configure the context.
186184

@@ -201,7 +199,7 @@ The factory is injected into components and used to create new instances. For ex
201199
> [!NOTE]
202200
> `Wrapper` is a [component reference](xref:blazor/components/index#capture-references-to-components) to the `GridWrapper` component. See the `Index` component (`Pages/Index.razor`) in the [sample app](https://github.com/dotnet/AspNetCore.Docs/blob/master/aspnetcore/blazor/common/samples/3.x/BlazorServerEFCoreSample/BlazorServerDbContextExample/Pages/Index.razor).
203201
204-
### Scope to the component lifetime
202+
<h3 id="scope-to-the-component-lifetime-3x">Scope to the component lifetime</h3>
205203

206204
You may wish to create a <xref:Microsoft.EntityFrameworkCore.DbContext> that exists for the lifetime of a component. This allows you to use it as a [unit of work](https://martinfowler.com/eaaCatalog/unitOfWork.html) and take advantage of built-in features, such as change tracking and concurrency resolution.
207205
You can use the factory to create a context and track it for the lifetime of the component. First, implement <xref:System.IDisposable> and inject the factory as shown in `Pages/EditContact.razor`:

0 commit comments

Comments
 (0)