实体框架自动迁移多个上下文 [英] Entity Framework Automated Migration multiple context
问题描述
我有两个数据库.当我对一个dbcontext进行更改(添加新的DbSet)时,当运行Web应用程序时,它应该自动将更改应用于正确的数据库.因此,应添加新表.因此,我添加了两个配置类.每个数据库/上下文一个.
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>());
我也在web.config中尝试过
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
通过程序包管理器应用更改时,它将按应有的方式工作.该表被添加到我的domaincontext数据库中.
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.
因此而不是以下内容:
Solution/
DomainReadModels/
DomainContext.cs
Classes........
WebsiteReadModels/
WebsiteContext.cs
Classes........
Website/
Migration/
AddSomeTableToDomainMigration.cs
AddSomeTableToWebsiteMigration.cs
WebsiteReadModelsConfiguration.cs
DomainReadModelsConfiguration.cs
Websitefiles...
我将解决方案更改为:
Solution/
DomainReadModels/
Migration/
DomainReadModelsConfiguration.cs
AddSomeTableToDomainMigration.cs
DomainContext.cs
Classes........
WebsiteReadModels/
Migration/
AddSomeTableToWebsiteMigration.cs
WebsiteReadModelsConfiguration.cs
WebsiteContext.cs
Classes........
Website/
Websitefiles...
现在唯一的缺点是我必须在程序包管理器控制台中切换项目...
Now the only disadvantage is I have to switch projects in the package manager console...
这篇关于实体框架自动迁移多个上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!