dotnet / dotnet/EntityFramework.Docs

Examples with Blazor Server, Identity DbContextFactory (and optionally identity + multi-tenancy)

Open
#4,258 0 comments 0 reactions 0 assignees View on GitHub
area-context-management
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

## Ask a question

There are no examples that show how to configure and use EF core 7.x in Blazor Server using Identity with DbContextFactory.

Currently I have this as Program.cs:

```csharp

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.EntityFrameworkCore;
using Winking.Optisend.Mail;
using Winking.Optisend.Mail.Areas.Identity;
using Winking.Optisend.Mail.Core.Data;
using Winking.Optisend.Mail.Data;

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext(options =>
options.UseSqlServer(connectionString), ServiceLifetime.Singleton); // <-- is singleton ok here? Without it doesn't work.
#if DEBUG
builder.Services.AddDbContextFactory(opt => opt.UseSqlServer(connectionString).EnableSensitiveDataLogging(true));
#else
builder.Services.AddDbContextFactory(opt => opt.UseSqlServer(connectionString));
#endif
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddDevExpressBlazor();
builder.Services.AddScoped>();
//builder.Services.AddSingleton();
builder.Services.Configure(options =>
{
options.BootstrapVersion = DevExpress.Blazor.BootstrapVersion.v5;
});

// should these 2 be added (from .net 6 project template)
builder.WebHost.UseWebRoot("wwwroot");
builder.WebHost.UseStaticWebAssets();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

```

When looking in the documentation, it is not clear if this configuration above is correct or not for Blazor Server (e.g. regarding the factory and `serviceLifeTime.Singleton`):

When I remove the ServiceLifeTime.Singleton I get things like:
```
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.IDbContextFactory`1....
```

- https://learn.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-7.0#database-access
- https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/#using-a-dbcontext-factory-eg-for-blazor
- https://learn.microsoft.com/en-us/ef/core/miscellaneous/multitenancy#blazor-server-apps-and-the-life-of-the-factory

Any examples on how to configure ef core + dbfactory + blazor server using multi-tenancy + **identity** (having Multiple databases and connection strings)?

https://learn.microsoft.com/en-us/ef/core/miscellaneous/multitenancy#multiple-databases-and-connection-strings

We've all seen the basic examples creating a Blog with Posts and Tags but once you go to just beyond this, you end up in the desert 🏜️ 🐪 .

### Include provider and version information

EF Core version: 7
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7
Operating system:
IDE: Visual Studio 2022 17.4.5

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.