- 准备
首先安装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") { } } }
- 添加Migrations文件夹
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
大功告成
时间: 2024-10-25 08:01:20