How to Enable Entity Framework Migrations for MembershipReboot

213 Views Asked by At

We have implemented IdentityServer3, MembershipReboot and IdentityManager, and are storing Clients, Scopes and Users in a SqlServer database. We’ve also set up default users during initial database creation using the CustomUserAccountService class. Now, we want to add an additional field to the UserAccount table, and don’t want to recreate the database from scratch, so we’re hoping to enable Entity Framework migrations for the UserAccount table. When we enabled EntityFramework for IdentityServer, we used the following commands:

Enable-Migrations -MigrationsDirectory Migrations\ClientConfiguration -ContextTypeName ClientConfigurationDbContext -ContextAssemblyName IdentityServer3.EntityFramework -ConnectionStringName IdSvr3AndMembershipReboot

Enable-Migrations -MigrationsDirectory Migrations\ScopeConfiguration -ContextTypeName ScopeConfigurationDbContext -ContextAssemblyName IdentityServer3.EntityFramework -ConnectionStringName IdSvr3AndMembershipReboot

Enable-Migrations -MigrationsDirectory Migrations\OperationalConfiguration -ContextTypeName OperationalDbContext -ContextAssemblyName IdentityServer3.EntityFramework -ConnectionStringName IdSvr3AndMembershipReboot

We looked in MembershipReboot, and the DbContext we see there is called MembershipRebootDbContext, so we attempted the following:

Enable-Migrations -MigrationsDirectory Migrations\MembRebootConfiguration -ContextTypeName MembershipRebootDbContext -ContextAssemblyName BrockAllen.MembershipReboot.Ef -ConnectionStringName IdSvr3AndMembershipReboot

However, that returned this error:

The context type 'MembershipRebootDbContext' was not found in the assembly 'BrockAllen.MembershipReboot.Ef'

We decompiled the assembly, and there is definitely a type with that name in the assembly, but we’re thinking the issue may be that it’s a generic type, unlike the Client, Scope and Operational DbContexts, which are not generic types.

Is it possible to enable Entity Framework migrations for MembershipReboot? What commands are needed?

Many thanks for your help!

1

There are 1 best solutions below

0
On

I created a sub-class of the generic MembershipRebootDbContext with a concrete TUserAccount class. That I added a default constructor which passes a connectionstring name to the base constructor.

public class MyMembershipRebootDbContext : MembershipRebootDbContext<User>
{
    public MyMembershipRebootDbContext() : base("IdentityServerMembershipDb") {}

    public MyMembershipRebootDbContext(string name)
        : base(name) {}
}

With that done you should be able to create the migrations as follows:

Enable-Migrations -MigrationsDirectory Migrations\MembRebootConfiguration -ContextTypeName MyMembershipRebootDbContext -StartUpProjectName MyProject -ProjectName MyProject -ConnectionStringName IdSvr3AndMembershipReboot