因為有時候根據項目的不同,可能會需要多個庫來保存數據。之前使用code-first都是根據單個數據庫來生成,為此百度了一番之后,找到了這篇文章通過Migration在EF6中用多個DbContext
為此記錄一下。
Enable-Migrations -ContextTypeName AbpFramewok.Infrastructure.EnterpriseDbContext -MigrationsDirectory:Migrations\Enterprise
ContextTypeName:DbContext的位置,即DbContext的命名空間加上DbContext的名稱
MigrationsDirectory:需要遷移到的目標文件夾因為多個Dbcontext的遷移文件還是分開來放比較好、直觀,另外的Dbcontext也是如此
Enable-Migrations -ContextTypeName AbpFramewok.Infrastructure.JobseekerDbContext -MigrationDirectory:Migrations\Jobseeker
添加遷移腳本,因為我們有多個Dbcontext,所以add-migration 需要指定Dbcontext的Configuration的位置
add-migration -ConfigurationTypeName AbpFramewok.Infrastructure.Migrations.Jobseeker.Configuration InitJobseekerDb
-ConfigurationTypeName 指定configuration的路徑
同理 Update-database也需要指定Dbcontext
update-database -ConfigurationTypeName AbpFramewok.Infrastructure.Migrations.Jobseeker.Configuration