时间:2021-07-01 10:21:17 帮助过:22人阅读
首先安装entity framework ,
Install-Package EntityFramework
接着创建两个我们自己的context,
MyFirstContext.cs
using System.Data.Entity; namespace Question.EntityFramework { public class MyFirstContext : DbContext { public MyFirstContext() : base("MyFirst") { } } }
MySecondContext.cs
using System.Data.Entity; namespace Question.EntityFramework { public class MySecondContext : DbContext { public MySecondContext() : base("MySecond") { } } }
enable-migrations -ContextTypeName Question.EntityFramework.MyFirstContext -MigrationsDirectory FirstMigrations
enable-migrations -ContextTypeName Question.EntityFramework.MySecondContext -MigrationsDirectory SecondMigrations
ContextTypeName:需要迁移的数据库(类)
MigrationsDirectory:指定迁移文件的目录
创建2个实体类,MyFirstEntity.cs和MySecondEntity.cs
然后继续在程序包管理器控制台中输入:
add-migration Initial -ConfigurationTypeName Question.EntityFramework.FirstMigrations.Configuration
add-migration Initial -ConfigurationTypeName Question.EntityFramework.SecondMigrations.Configuration
-ConfigurationTypeName:指定使用的迁移配置 对应不同的文件夹下的不同命名空间的迁移配置
最后配置好web.config输入:
update-database -ConfigurationTypeName Question.EntityFramework.FirstMigrations.Configuration
update-database -ConfigurationTypeName Question.EntityFramework.SecondMigrations.Configuration
大功告成
EF CodeFirst,同项目多个数据库的迁移
标签: