I have two databases. When I make a change to one of my dbcontext (adding a new DbSet) it should automatically apply the changes to the correct databases when runnnig my webapplication. So the new table should be added. Therefore I have added two configuration classes. One for each database/context.
However using the initializers like below the changes are always applied to the second context/database. Because this is the latest initializer configured.
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DomainReadModelContext, DomainConfiguration>());
Database.SetInitializer(new MigrateDatabaseToLatestVersion<WebsiteReadModelContext, WebsiteConfiguration>());
I also tried it in the web.config
<contexts>
<context type="Domain.ReadModels.DomainReadModelContext, Domain.ReadModels">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[Domain.ReadModels.DomainReadModelContext, Domain.ReadModels], [Website.Migrations.Domain.DomainConfiguration, Website-WebAppMainModule, Version=1.0.0.0, Culture=neutral]], EntityFramework" />
</context>
<context type="Website.ReadModels.WebsiteReadModelContext, Website.ReadModels">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[Website.ReadModels.WebsiteReadModelContext, Website.ReadModels], [Website.Migrations.Website.WebsiteConfiguration, Website-WebAppMainModule, Version=1.0.0.0, Culture=neutral]], EntityFramework" />
</context>
</contexts
When applying the changes via the package manager it works as it should be. The table gets added to my domaincontext database.
Update-Database -config DomainConfiguration
Is this because this isn’t supported or am I doing it wrong? Now it seems to work only for the latest initializer registered.
For the update I have scaffolded a Migration using the Add-Migration command in the package manager.
Add-Migration AddUniquePersonReadModelMigration -config DomainConfiguration
This generated following class for me.
public partial class AddUniquePersonReadModelMigration : DbMigration
{
public override void Up()
{
CreateTable(
"UniquePersonReadModels",
c => new
{
Id = c.Guid(nullable: false, identity: true),
PersonId = c.Guid(nullable: false),
DisplayName = c.String(maxLength: 128),
})
.PrimaryKey(t => t.Id)
.Index(p => p.PersonId)
.Index(p => p.DisplayName, true);
}
public override void Down()
{
DropIndex("UniquePersonReadModels", new[] { "PersonId" });
DropIndex("UniquePersonReadModels", new[] { "DisplayName" });
DropTable("UniquePersonReadModels");
}
}
So my question is does entity framework support the migrations for multiple contexts using initializers? If not, it would be a nice feature when the migrations can be handled for multiple contexts.
I have solved the problem by moving the Migration folder from my website to separate assemblies.
So instead of the following:
I changed my solution to:
Now the only disadvantage is I have to switch projects in the package manager console…