Cant move ConfigurationDbContext or PersistedGrantDbContext into separate project
Issue / Steps to reproduce the problem
Setup Identity Server 4 to use ASP.Net Identity and to store Configuration and Persistent Grants in the DB. This works fine.
When trying to do a separation of concerns by separating the DBContexts into their own project eg. "IdentityServer4.Data", which had all the relevant references etc. The projects are able to build but when running the following code lines in the Pcakage Manager Console, i find the identity server contexts do not get applied to the DB: update-database -Context ApplicationDbContext update-database -Context PersistedGrantDbContext update-database -Context ConfigurationDbContext
The only context that gets applied to the DB is the ApplicationDbContext.
Output for the PersistendGrantDbContext is below
Relevant parts of the log file
PM> update-database -Context PersistedGrantDbContext Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0] User profile is available. Using 'C:\Users\Developer\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest. dbug: IdentityServer4.Startup[0] Using Identity.Application as default scheme for authentication dbug: IdentityServer4.Startup[0] Using Identity.External as default scheme for sign-in dbug: IdentityServer4.Startup[0] Using Identity.External as default scheme for sign-out dbug: IdentityServer4.Startup[0] Using Identity.Application as default scheme for challenge dbug: IdentityServer4.Startup[0] Using Identity.Application as default scheme for forbid Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 2.0.1-rtm-125 initialized 'PersistedGrantDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MigrationsAssembly=IdentitysServer4 Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT OBJECT_ID(N'__EFMigrationsHistory'); Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT OBJECT_ID(N'__EFMigrationsHistory'); Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [MigrationId], [ProductVersion] FROM [__EFMigrationsHistory] ORDER BY [MigrationId]; Microsoft.EntityFrameworkCore.Migrations[20405] No migrations were applied. The database is already up to date. No migrations were applied. The database is already up to date. Done.
<log goes here>
Fixed by adding + ".Data" tot he migrationsAssembly
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
// configure identity server with stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential()
//.AddTestUsers(Config.GetUsers())
// this adds the config data from DB (clients, resources)
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly + ".Data" ));
})
// this adds the operational data from DB (codes, tokens, consents)
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly + ".Data"));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30;
})
.AddAspNetIdentity<ApplicationUser>();
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly + ".Data"));
Hey @ChrisDevinePimss Can you please help. I am getting error: Your target project 'Project.Identity' doesn't match your migrations assembly 'Project.Data'.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.