Skip to content

Commit c24e918

Browse files
committed
Updates
1 parent 6da26b3 commit c24e918

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

aspnetcore/blazor/blazor-ef-core.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ In the preceding factory:
141141

142142
:::moniker-end
143143

144-
The following example configures [SQLite](https://www.sqlite.org/index.html) and enables data logging. The code uses an extension method (<xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContextFactory%2A>) to configure the database factory for DI and provide default options:
144+
The following example configures [SQLite](https://www.sqlite.org/index.html) and enables data logging in an app that manages contacts. The code uses an extension method (<xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContextFactory%2A>) to configure the database factory for DI and provide default options:
145145

146146
:::moniker range=">= aspnetcore-6.0"
147147

@@ -161,15 +161,17 @@ services.AddDbContextFactory<ContactContext>(opt =>
161161

162162
:::moniker-end
163163

164-
The factory is injected into components and used to create new `DbContext` instances.
164+
The factory is injected into components to create new <xref:Microsoft.EntityFrameworkCore.DbContext> instances.
165165

166-
`IDbContextFactory<ContactContext>` is injected into the component:
166+
## Scope a database context to a component method
167+
168+
The factory is injected into the component:
167169

168170
```razor
169171
@inject IDbContextFactory<ContactContext> DbFactory
170172
```
171173

172-
To delete a contact in a Razor component, `DbContext` is created using the factory (`DbFactory`):
174+
Create a <xref:Microsoft.EntityFrameworkCore.DbContext> for a method using the factory (`DbFactory`):
173175

174176
```csharp
175177
private async Task DeleteContactAsync()
@@ -189,33 +191,38 @@ private async Task DeleteContactAsync()
189191
}
190192
```
191193

192-
## Scope to the component lifetime
194+
## Scope a database context to the lifetime of the component
193195

194196
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.
195197

196-
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 into the component:
198+
Implement <xref:System.IDisposable> and inject the factory into the component:
197199

198200
```razor
199201
@implements IDisposable
200202
@inject IDbContextFactory<ContactContext> DbFactory
201203
```
202204

203-
The context is disposed when the component is disposed:
205+
Establish a property for the <xref:Microsoft.EntityFrameworkCore.DbContext>:
204206

205207
```csharp
206-
public void Dispose() => Context?.Dispose();
208+
private ContactContext? Context { get; set; }
207209
```
208210

209-
Finally, [`OnInitializedAsync`](xref:blazor/components/lifecycle) is overridden to create a new context and load the contact:
211+
[`OnInitializedAsync`](xref:blazor/components/lifecycle) is overridden to create the <xref:Microsoft.EntityFrameworkCore.DbContext>:
210212

211213
```csharp
212214
protected override async Task OnInitializedAsync()
213215
{
214216
Context = DbFactory.CreateDbContext();
215-
Contact = await Context.Contacts.SingleOrDefaultAsync(...);
216217
}
217218
```
218219

220+
The <xref:Microsoft.EntityFrameworkCore.DbContext> is disposed when the component is disposed:
221+
222+
```csharp
223+
public void Dispose() => Context?.Dispose();
224+
```
225+
219226
## Enable sensitive data logging
220227

221228
<xref:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.EnableSensitiveDataLogging%2A> includes application data in exception messages and framework logging. The logged data can include the values assigned to properties of entity instances and parameter values for commands sent to the database. Logging data with <xref:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.EnableSensitiveDataLogging%2A> is a **security risk**, as it may expose passwords and other [Personally Identifiable Information (PII)](xref:blazor/security/index#personally-identifiable-information-pii) when it logs SQL statements executed against the database.

0 commit comments

Comments
 (0)