EF--code first数据迁移命令
2024-10-17 11:34:18
原文推荐!点我点我!
添加Migrations文件夹,并生成类文件Configuration.cs
PM> Enable-Migrations -EnableAutomaticMigrations
在Migrations文件夹中新增类文件201309201556388_InitialCreate.cs
Add-Migration InitialCreate //后面的名字是随便写的
执行结果生成与上面一致的数据库
Update-Database -Verbose
数据库模型中添加City类,执行程序包管理器控制台语句,Migrations文件夹中新增类文件201309201643300_AddCity.cs
Add-Migration AddCity //后面的名字是随便写的
执行程序包管理器控制台语句,进行数据库版本回溯
Update-Database –TargetMigration:"201309201643300_AddCity"
生成数据库版本之间的Sql脚本
Update-Database -Script -SourceMigration:"201309201643300_AddCity" -TargetMigration:"201309201708043_ModifyCity"
执行Sql脚本
Update-Database -Script
Enable-Migrations指定项目名称
Enable-Migrations -StartUpProjectName Portal
MigrateDatabaseToLatestVersion:将Entity的修改同步到数据库
下面的代码的原文地址
public class StyleContext:DbContext
{
public StyleContext() : base("name=WMSAidDB")
{
//自动创建表,如果Entity有改到就更新到表结构
Database.SetInitializer<StyleContext>(new MigrateDatabaseToLatestVersion<StyleContext, ReportingDbMigrationsConfiguration>());
}
public DbSet<StyleDetail> StyleDetails { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
internal sealed class ReportingDbMigrationsConfiguration : DbMigrationsConfiguration<StyleContext>
{
public ReportingDbMigrationsConfiguration()
{
AutomaticMigrationsEnabled = true;//任何Model Class的修改將會直接更新DB
AutomaticMigrationDataLossAllowed = true;
}
}
最新文章
- [OSG][osgEarth]osgEarth例子程序简介
- 使用 Fluent API 配置/映射属性和类型(摘自微软Data Access and Storage)
- C# Mysql You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ????
- 如何优雅的使用vue+vux开发app -03
- 创建Oracle数据库
- Express 4 更新改变文档
- 转 关于C#中派生类调用基类构造函数的理解
- Fragment 与 Activity 通信
- 在ViewController中View的生命周期
- 使用cordova开发app
- Mina源码阅读笔记(二)- IoBuffer的封装
- phpexcel 使用模板导出报表
- HDU 4687 Boke and Tsukkomi (一般图最大匹配)【带花树】
- linux基本网络配置
- Web开发.net framework 类库中必须掌握的命名空间(或者类)【转】
- Intersection Observer API 可以让你知道被观察元素何时进入或退出浏览器的视口
- NT1_keras下搭建一个3层模型并且修改。
- json和java bean的相互转换(使用fastjson)
- 看我如何快速学习.Net(高可用数据采集平台)
- 邂逅明下(巴什博弈+hdu2897)