Search code examples
entity-frameworkasp.net-coreef-code-firstidentityserver4asp.net-core-identity

How to share DbContexts and not get 2 user tables


I'm using Asp.Net Core 3.1 with Entity Framework, Asp.Net Identity, and IdentityServer4. I'm trying to share my DbContexts across several projects but my code first migrations aren't producing what I need. Is this possible with code first migrations? Is my setup completely asinine? Thank you for your time.


My project hierarchy with DbContexts.

Project Heirarchy

What I want

Entity Relationship Goal

What I get

Entity Relationship Actual

In both Identity and Application Startups

services.AddIdentity<AppUser, IdentityRole>()
        .AddEntityFrameworkStores<AppUserContext>()
        .AddDefaultTokenProviders();
...
services.AddDbContext<AppUserContext>(options => options.UseSqlServer(connectionString));

Migrations

dotnet-ef migrations add Init -p User\User.csproj -c AppUserContext -s IdentityServer\IdentityServer.csproj
dotnet-ef migrations add Init -p Persistence\Persistence.csproj -c RecipeContext -s Application\Application.csproj

Update 1

Moving AppUser and AppUserContext to IdentityServer. Red is a restricted endpoint.

Update 1 Sequence


Solution

  • Thanks to @RuardvanElburg's advice, I "solved" this by changing my paradigm which you can see in my Update 1.