IdentityServer / IdentityServer3.EntityFramework

EntityFramework persistence layer for IdentityServer3
Apache License 2.0
68 stars 97 forks source link

Turn off Migrations / Database Initializer #96

Closed BruceHunter closed 8 years ago

BruceHunter commented 8 years ago

What is the recommened way you suggest to turn off the migrations / Database Initilizer for (ClientConfigurationDbContext, OperationalDbContext and ScopeConfigurationDbContext) ?

We want to control the schema etc through Database projects. I do not agree that EF migrations in 6 is ready for prime time. Maybe EF 7 will resolve what I'm seeing.

BruceHunter commented 8 years ago

The only way I've found to do this is the following. Create your own Configuration classes.

    public class ClientConfigurationDbContext: BaseDbContext, IClientConfigurationDbContext
    {
        public ClientConfigurationDbContext(): base(Constants.IdentityDbConnectionStringName, "dbo")
        {
           Database.SetInitializer<ClientConfigurationDbContext>(null);
        }
        public override int SaveChanges()
        {
            return base.SaveChanges();
        }
        public override Task<int> SaveChangesAsync()
        {
            return base.SaveChangesAsync();
        }
        protected override void ConfigureChildCollections()
        {
            //this.RegisterClientChildTablesForDelete<Client>();
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //base.OnModelCreating(modelBuilder);
            //modelBuilder.ConfigureClients(Schema);
        }

        public DbSet<Client> Clients { get; set; }
    }
BruceHunter commented 8 years ago

I do not think it's a good idea to turn off RegisterClientChildTablesForDelete or any of the Registration Delete events. However, if this line of code is on then you can't turn off Migrations. Not sure what I should do.

Maybe this can work? [https://coding.abel.nu/2012/03/prevent-ef-migrations-from-creating-or-changing-the-database/]

BruceHunter commented 8 years ago

The solution is to call these in your Global.asax in Application_Start or as I did put them in the IAppBuilder Extension for IdentityServer app.Map.

Put these first and not inside the Context classes cause it won't work if you do.

Database.SetInitializer<ClientConfigurationDbContext>(null);
Database.SetInitializer<ScopeConfigurationDbContext>(null);
Database.SetInitializer<OperationalDbContext>(null);
BruceHunter commented 8 years ago

I'm going to close this.

rahatur commented 7 years ago

Adding the following to my Startup.cs worked:

public void Configuration(IAppBuilder appBuilder)
{
  Database.SetInitializer<ClientConfigurationDbContext>(null);
  Database.SetInitializer<ScopeConfigurationDbContext>(null);
  Database.SetInitializer<OperationalDbContext>(null);
}