Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117046

IConfigurationDbContext returning missing columns

I am trying to search for valid clients in on my Identity server. Its currently runnning identityserver4.

My code:

    // GET api/developerconsole/{clientId}
    [HttpGet("{clientId}")]
    public async Task<ActionResult> Get(string clientId)
    {
        try
        {
            var client = _configurationDbContext.Clients.Find(1);  // Fails here
            return Ok(Newtonsoft.Json.JsonConvert.SerializeObject(client));
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }

    }

The error

Invalid column name 'BackChannelLogoutSessionRequired'.
Invalid column name 'BackChannelLogoutUri'.
Invalid column name 'ClientClaimsPrefix'.
Invalid column name 'ConsentLifetime'.
Invalid column name 'Description'.
Invalid column name 'FrontChannelLogoutSessionRequired'.
Invalid column name 'FrontChannelLogoutUri'.
Invalid column name 'PairWiseSubjectSalt'.

ConfigurationDbContext injection

 // IDS
        services.AddEntityFrameworkSqlServer()
            .AddDbContext<ConfigurationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("XenaIdentityConnection")));

I don't understand why it would be missing columns

Update:

As suggested in comments I ran

dotnet ef migrations add updatedIdentityServer4Schema

It resulted in the following error:

An error occurred while calling method 'ConfigureServices' on startup class 'Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: Den angivne sti blev ikke fundet More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

Note:

dotnet ef migrations add updatedIdentityServer4Schema -c ConfigurationDbContext

No parameterless constructor was found on 'ConfigurationDbContext'. Either add a parameterless constructor to 'ConfigurationDbContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ConfigurationDbContext'.

Update 2:

It appears that the code in the project should support the migration its using this Using EntityFramework Core for configuration and operational data

The only difference i can see is that we request our user data from another database

services.AddDbContext<UserDbContext>(builder =>builder.UseSqlServer(Configuration.GetConnectionString("ConnectionToUserDB")));

Update :

I tried creating a idbcontextfactory

 public class ConfigurationFactory : IDbContextFactory<ConfigurationDbContext>
{
    public ConfigurationDbContext Create(DbContextFactoryOptions options)
    {
        var connectionString = "server=.\\sqlexpress;initial catalog=Xena.Identity2.0;Integrated Security=true";
        var optionsBuilder = new DbContextOptionsBuilder<ConfigurationDbContext>();
        optionsBuilder.UseSqlServer(connectionString, b => b.MigrationsAssembly("Xena.IdentityServer"));
        return new ConfigurationDbContext(optionsBuilder.Options, new ConfigurationStoreOptions());

    }
}

This allowed me to

dotnet ef migrations add updatedIdentityServer4Schema -c ConfigurationDbContext

An error occurred while calling method 'ConfigureServices' on startup class 'Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: Den angivne sti blev ikke fundet Executed DbCommand (47ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT OBJECT_ID(N'__EFMigrationsHistory'); Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT OBJECT_ID(N'__EFMigrationsHistory'); Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [MigrationId], [ProductVersion] FROM [__EFMigrationsHistory] ORDER BY [MigrationId]; Applying migration '20171201112112_updatedIdentityServer4Schema'. Executed DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'20171201112112_updatedIdentityServer4Schema', N'1.1.2'); Done.

after which i did an update. But the new columns have not appeared in my database.

Upvotes: 0

Views: 1789

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117046

Once the full Identity server project has been upgraded to 2.0. and builds there have been some breaking changes in IdentityServer4 2.0

I added the following

public class ConfigurationFactory : IDesignTimeDbContextFactory<ConfigurationDbContext>
    {
        public ConfigurationDbContext CreateDbContext(string[] args)
        {
            var connectionString = "server=.\\sqlexpress;initial catalog=Xena.Identity2.0;Integrated Security=true";
            var optionsBuilder = new DbContextOptionsBuilder<ConfigurationDbContext>();
            optionsBuilder.UseSqlServer(connectionString, b => b.MigrationsAssembly("Xena.IdentityServer"));
            return new ConfigurationDbContext(optionsBuilder.Options, new ConfigurationStoreOptions());
        }
    }

public class PersistedGrantDbContextFactory : IDesignTimeDbContextFactory<PersistedGrantDbContext>
    {
        public PersistedGrantDbContext CreateDbContext(string[] args)
        {
            var connectionString = "server=.\\sqlexpress;initial catalog=Xena.Identity2.0;Integrated Security=true";
            var optionsBuilder = new DbContextOptionsBuilder<PersistedGrantDbContext>();
            optionsBuilder.UseSqlServer(connectionString, b => b.MigrationsAssembly("Xena.IdentityServer"));
            return new PersistedGrantDbContext(optionsBuilder.Options, new OperationalStoreOptions());
        }
    }

Then i was able to preform

dotnet ef migrations add updatedIdentityServer4Schema -c ConfigurationDbContext
dotnet ef database update -c ConfigurationDbContext
dotnet ef migrations add updatedIdentityServer4Schema -c 
PersistedGrantDbContext
dotnet ef database update -c PersistedGrantDbContext

To update to the newest version of the database for IdentityServer4. I recommend fixing the above code to work with the config files which it doesn't do currently.

Upvotes: 3

Related Questions