Skip to content

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

Open
@wstaelens

Description

@wstaelens

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:

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<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString), ServiceLifetime.Singleton); // <-- is singleton ok here? Without it doesn't work.
#if DEBUG
builder.Services.AddDbContextFactory<ApplicationDbContext>(opt => opt.UseSqlServer(connectionString).EnableSensitiveDataLogging(true));
#else 
builder.Services.AddDbContextFactory<ApplicationDbContext>(opt => opt.UseSqlServer(connectionString));
#endif
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddDevExpressBlazor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
//builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.Configure<DevExpress.Blazor.Configuration.GlobalOptions>(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....

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions