code first , Migration
文章引用至: https://www.cnblogs.com/panchunting/p/entity-framework-code-first-migrations.html
随着业务的增加, 之前code first创建的表可能已经不能满足需求, 比如增加一个字段, 这样就出来了‘Migrations’
第一步:
- 在 Package Manager Console 下运行命令 Enable-Migrations
需要熟悉的命令有:
- Add-Migration 将 scaffold 创建下一次基于上一次迁移以来的更改的迁移;
- Update-Databse 将任何挂起的迁移应用到数据库
《1》: 新增字段: ‘Url’
1. console输入: Add-Migration AddNewUrl
2. 会看到产生一个新的cs文件

完善方法中的内容: (新增字段的类型需要自己定义, 如果为int型, 就是'c=>c.Int()')
public partial class AddNewUrl : DbMigration
{
public override void Up()
{
AddColumn("dbo.News", "Url", c => c.String());
} public override void Down()
{
DropColumn("dbo.News", "Url");
}
}
《2》 删除字段
1. console输入: Add-Migration DeleteNewUrl
2. 对应产生文件,完善文件中的方法
public partial class DeleteNewUrl : DbMigration
{
public override void Up()
{
DropColumn("dbo.News", "Url");
} public override void Down()
{
}
}
3 执行更新 : Update-Database
《3》修改字段 类型
1. console输入: Add-Migration UpdateRatingUrl
2. 对应产生文件,完善文件中的方法
public partial class UpdateRatingUrl : DbMigration
{
public override void Up()
{
AlterColumn("dbo.News", "Rating", c => c.String());
} public override void Down()
{
}
}
3 执行更新 : Update-Database
《4》新增一个集合
1. console输入: Add-Migration AddTagModel
2. 对应产生文件,完善文件中的方法
public partial class AddTagModel : DbMigration
{
public override void Up()
{
CreateTable("dbo.Tags", c => new
{
TagId = c.Int(nullable: false, identity: true),
TagName = c.String(maxLength: ),
NewId = c.Int(nullable: false)
}).PrimaryKey(t => t.TagId)
.ForeignKey("dbo.News", t => t.NewId, cascadeDelete: true)
.Index(t => t.NewId)
.Index(p => p.TagName, unique: true);
} public override void Down()
{
DropIndex("dbo.Tags", new[] { "TagName" });
DropForeignKey("dbo.Tags", "NewId", "dbo.News");
DropIndex("dbo.Tags", new[] { "NewId" });
DropTable("dbo.Tags");
}
}
3 执行更新 : Update-Database
4 执行结果

code first , Migration的更多相关文章
- EF Code First Migration总结
开启Migration 1. 通过 Tools->Nuget Package Manager->Package Manager Console 打开Package Manager Cons ...
- Some lines about EF Code First migration.
Some lines about EF Code First migration: 一. 模型设计 1. 遵循EF标准,注意表关系配对 2. 数据模型里尽量把必须的属性和说明都写全 3. EF默认id ...
- MVC - Code First Migration Command line
当开发MVC应用程序, 使用.NET Entity Framework的Code First model试, 若是需要将model层对象的改动更新进数据库, 需要使用Package Manager C ...
- 解决code first Migration 增加外键时出现错误的问题
先上模型 Comment public class Comment { [Key] public int CommentId { get; set; } [Required] public int S ...
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...
- Entity Framework Code First 学习
1.添加entityframework 项目-管理解决方案的 NuGet 程序包-联机-Entity Framework 2.code first Migration 工具->库程序包管理器-& ...
- MVC 5 的 EF6 Code First 入门
英文渣水平,大伙凑合着看吧…… 这是微软官方SignalR 2.0教程Getting Started with Entity Framework 6 Code First using MVC 5 系列 ...
- 在Code First中使用Migrations对实体类和数据库做出变更
在Code First中使用Migrations对实体类和数据库做出变更,Mirgration包含一系列命令. 工具--库程序包管理器--程序包管理器控制台 运行命令:Enable-Migration ...
- C# ORM—Entity Framework 之Code first(代码优先)(二)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
随机推荐
- C# 字符串转为DateTime类型
方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...
- 生产者消费者synchronized wait notify
package ProduceQueueProduce; import java.util.Queue; public class ProducerThread extends Thread { pu ...
- (23/24) webpack实战技巧:如何在webpack环境中使用Json
在webpack1或者webpack2版本中,若想在webpack环境中加载Json文件,则需要加载一个json-loader的loader进来的.但是在webpack3.x版本中,则不需要在另外引入 ...
- sys模块学习记录
import sys s = sys.argv #命令行参数List,第一个元素是程序本身路径 #sys.exit() #退出程序,正常退出时exit(0) s = sys.version #获取Py ...
- python目录结构
import sys,os #__file__取得当前文件名,pycharm会自动加上完整路径 #os.path.dirname取得上一级目录 #os.path.abspath取得绝对路径 BASE_ ...
- Delphi中Chrome Chromium、Cef3学习笔记(五)
原文 http://blog.csdn.net/xtfnpgy/article/details/48489489 一.模拟移动鼠标 // SetCursorPos(StrToInt(Edit ...
- 【COCI 2015/2016 #3】Nekameleoni
题目描述 “这好难啊,我有一个简单点的题,他们解决不了.” AKPAKP有一个长度为nn的线段,这个线段原来染有颜色,AKPAKP只认识kk种颜色.当然原来的颜色也包含在着kk种颜色之间. 可以进行m ...
- Tomcat 启动时 SecureRandom 非常慢解决办法,亲测有效
1.找到jre—>lib—>security 2.找到 securerandom.source=file:/dev/random 替换成:securerandom.source= ...
- ReactiveX 学习笔记(28)使用 RxJS + React.js 进行 GUI 编程
课题 程序界面由3个文本编辑框和1个文本标签组成. 要求文本标签实时显示3个文本编辑框所输入的数字之和. 文本编辑框输入的不是合法数字时,将其值视为0. 3个文本编辑框的初值分别为1,2,3. 创建工 ...
- R数据导入导出(一): read.table()和read.csv()的区别
之前也参考过一些资料,虽然是这么简单的两个buildin,还是仔细对比了一下,我有两张txt,都是从cube中导出的,就意味着每一列的列数是不一样的.R语言官方文档中有这样一句话不知道大家注意到了没有 ...