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: aspnetcore/blazor/blazor-ef-core.md
+17-10Lines changed: 17 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -141,7 +141,7 @@ In the preceding factory:
141
141
142
142
:::moniker-end
143
143
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:
## Scope a database context to the lifetime of the component
193
195
194
196
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.
195
197
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:
The <xref:Microsoft.EntityFrameworkCore.DbContext> is disposed when the component is disposed:
221
+
222
+
```csharp
223
+
publicvoidDispose() =>Context?.Dispose();
224
+
```
225
+
219
226
## Enable sensitive data logging
220
227
221
228
<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