Skip to content

Commit bf29785

Browse files
authored
Blazor Server-EF Core factory with Identity (#20169)
1 parent 90ac053 commit bf29785

3 files changed

Lines changed: 62 additions & 4 deletions

File tree

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ 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+
New <xref:Microsoft.EntityFrameworkCore.DbContext> instances can be created with a factory that allows you to configure the connection string per `DbContext`, such as when you use [ASP.NET Core's Identity model])(xref:security/authentication/customize_identity_model):
98+
99+
```csharp
100+
services.AddDbContextFactory<ApplicationDbContext>(options =>
101+
{
102+
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
103+
});
104+
105+
services.AddScoped<ApplicationDbContext>(p =>
106+
p.GetRequiredService<IDbContextFactory<ApplicationDbContext>>()
107+
.CreateDbContext());
108+
```
109+
97110
<h3 id="scope-to-the-component-lifetime-5x">Scope to the component lifetime</h3>
98111

99112
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.
@@ -112,6 +125,23 @@ Finally, [`OnInitializedAsync`](xref:blazor/components/lifecycle) is overridden
112125

113126
[!code-csharp[](./common/samples/5.x/BlazorServerEFCoreSample/BlazorServerDbContextExample/Pages/EditContact.razor?name=snippet2)]
114127

128+
<h3 id="enable-sensitive-data-logging">Enable sensitive data logging</h3>
129+
130+
<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) when it logs SQL statements executed against the database.
131+
132+
We recommend only enabling <xref:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.EnableSensitiveDataLogging%2A> for development and testing:
133+
134+
```csharp
135+
#if DEBUG
136+
services.AddDbContextFactory<ContactContext>(opt =>
137+
opt.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db")
138+
.EnableSensitiveDataLogging());
139+
#else
140+
services.AddDbContextFactory<ContactContext>(opt =>
141+
opt.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db"));
142+
#endif
143+
```
144+
115145
:::moniker-end
116146

117147
:::moniker range="< aspnetcore-5.0"
@@ -202,6 +232,19 @@ The factory is injected into components and used to create new instances. For ex
202232
> [!NOTE]
203233
> `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).
204234
235+
New <xref:Microsoft.EntityFrameworkCore.DbContext> instances can be created with a factory that allows you to configure the connection string per `DbContext`, such as when you use [ASP.NET Core's Identity model])(xref:security/authentication/customize_identity_model):
236+
237+
```csharp
238+
services.AddDbContextFactory<ApplicationDbContext>(options =>
239+
{
240+
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
241+
});
242+
243+
services.AddScoped<ApplicationDbContext>(p =>
244+
p.GetRequiredService<IDbContextFactory<ApplicationDbContext>>()
245+
.CreateDbContext());
246+
```
247+
205248
<h3 id="scope-to-the-component-lifetime-3x">Scope to the component lifetime</h3>
206249

207250
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.
@@ -225,6 +268,23 @@ In the preceding example:
225268
* When `Busy` is set to `true`, asynchronous operations may begin. When `Busy` is set back to `false`, asynchronous operations should be finished.
226269
* Place additional error handling logic in a `catch` block.
227270

271+
<h3 id="enable-sensitive-data-logging">Enable sensitive data logging</h3>
272+
273+
<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) when it logs SQL statements executed against the database.
274+
275+
We recommend only enabling <xref:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.EnableSensitiveDataLogging%2A> for development and testing:
276+
277+
```csharp
278+
#if DEBUG
279+
services.AddDbContextFactory<ContactContext>(opt =>
280+
opt.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db")
281+
.EnableSensitiveDataLogging());
282+
#else
283+
services.AddDbContextFactory<ContactContext>(opt =>
284+
opt.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db"));
285+
#endif
286+
```
287+
228288
:::moniker-end
229289

230290
## Additional resources

aspnetcore/blazor/common/samples/3.x/BlazorServerEFCoreSample/BlazorServerDbContextExample/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public void ConfigureServices(IServiceCollection services)
2626
// register factory and configure the options
2727
#region snippet1
2828
services.AddDbContextFactory<ContactContext>(opt =>
29-
opt.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db")
30-
.EnableSensitiveDataLogging());
29+
opt.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db"));
3130
#endregion
3231

3332
// pager

aspnetcore/blazor/common/samples/5.x/BlazorServerEFCoreSample/BlazorServerDbContextExample/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public void ConfigureServices(IServiceCollection services)
2626
// register factory and configure the options
2727
#region snippet1
2828
services.AddDbContextFactory<ContactContext>(opt =>
29-
opt.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db")
30-
.EnableSensitiveDataLogging());
29+
opt.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db"));
3130
#endregion
3231

3332
// pager

0 commit comments

Comments
 (0)