EF6中多个DbContext使用Code-First


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

为此记录一下。

Enable-Migrations -ContextTypeName AbpFramewok.Infrastructure.EnterpriseDbContext -MigrationsDirectory:Migrations\Enterprise

ContextTypeNameDbContext的位置,即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 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM