EF6中多个DbContext使用Code-First

因为有时候根据项目的不同,可能会需要多个库来保存数据。之前使用code-first都是根据单个数据库来生成,为此百度了一番之后,找到了这篇文章通过Migration在EF6中用多个DbContext

为此记录一下。

Enable-Migrations -ContextTypeName AbpFramewok.Infrastructure.EnterpriseDbContext -MigrationsDirectory:MigrationsEnterprise

ContextTypeNameDbContext的位置,即DbContext的命名空间加上DbContext的名称

MigrationsDirectory:需要迁移到的目标文件夹因为多个Dbcontext的迁移文件还是分开来放比较好、直观,另外的Dbcontext也是如此

Enable-Migrations -ContextTypeName AbpFramewok.Infrastructure.JobseekerDbContext -MigrationDirectory:MigrationsJobseeker

添加迁移脚本,因为我们有多个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 
原文地址:https://www.cnblogs.com/superfeeling/p/13948835.html