Code First Migrations更新数据库结构(数据迁移) 【转】
注意:一旦正常后,每次数据库有变化,做如下两步:
1. Enable-Migrations
2.update-database
背景
code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的旧数据库中包含一些测试数据时,当持久化更新后,原数据将全部丢失,故我们可以引入EF的数据迁移功能来完成。
- 已安装NuGet
- //原model
//原model
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- public class Lesson {
- public int lessonID { get; set; }
- [Required]
- [MaxLength(50)]
- public string lessonName { get; set; }
- [Required]
- public string teacherName { get; set; }
- public virtual UserInfo UserInfo{get;set;}
- }
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class Lesson {
public int lessonID { get; set; }
[Required]
[MaxLength(50)]
public string lessonName { get; set; }
[Required]
public string teacherName { get; set; }
public virtual UserInfo UserInfo{get;set;}
}
- //新model
//新model
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- public class Lesson {
- public int lessonID { get; set; }
- [Required]
- [MaxLength(50)]
- public string lessonName { get; set; }
- [Required]
- [MaxLength(10)]
- public string teacherName { get; set; }
- public virtual UserInfo UserInfo{get;set;}
- }
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class Lesson {
public int lessonID { get; set; }
[Required]
[MaxLength(50)]
public string lessonName { get; set; }
[Required]
[MaxLength(10)]
public string teacherName { get; set; }
public virtual UserInfo UserInfo{get;set;}
}
注:区别在于,我们给teacherName属性加了一个长度限制。
接下来,我们将开始持久化此model至数据库中(我们现在只是对属性作修改,此时数据库中此字段的长度为nvarchar(max),并不是nvarchar(10))
1:在config中配置数据库连接:
- <connectionStrings>
- <add name="TestUsersDB" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestUsersDB;Data Source=XCL-PC\SQLEXPRESS" providerName="System.Data.SqlClient" />
- </connectionStrings>
<connectionStrings>
<add name="TestUsersDB" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestUsersDB;Data Source=XCL-PC\SQLEXPRESS" providerName="System.Data.SqlClient" />
</connectionStrings>
2:打开NuGet控制台:

3:运行命令Enable-Migrations
可能会出现如下错误:
Checking if the context targets an existing database... Detected database created with a database initializer. Scaffolded migration '201212090821166_InitialCreate' corresponding to existing database. To use an automatic migration instead, delete the Migrations folder and re-run Enable-Migrations specifying the -EnableAutomaticMigrations parameter. Code First Migrations enabled for project MvcApplication1.
此时项目会出现如下文件夹:

打开configuation.cs,将作出如下修改:
- public Configuration()
- {
- AutomaticMigrationsEnabled = true;
- }
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
再次执行Update-Database:
因为我把长度从max改为10,在更新数据结构时,它认为此操作会导致数据丢失,如下:
Specify the '-Verbose' flag to view the SQL statements being applied to the target database. No pending code-based migrations. Applying automatic migration: 201212090848057_AutomaticMigration. Automatic migration was not applied because it would result in data loss.
如果确保没事,只需给此命令加个强制执行的参数即可:
Enable-Migrations -Force
最后再次执行:Update-Database

数据库中的原数据也没有丢失!
3:
Code First Migrations更新数据库结构(数据迁移) 【转】的更多相关文章
- ASP.NET MVC4 新手入门教程特别篇之一----Code First Migrations更新数据库结构(数据迁移)修改Entity FrameWork 数据结构(不删除数据)
背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的 ...
- Code First Migrations更新数据库结构(数据迁移)
背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建 (DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们 ...
- Code First Migrations更新数据库结构的具体步骤
一.打开程序包管理器控制台 当你的实体模型与数据库架构不一致时,引发以下错误:The model backingthe 'SchoolContext' context has changed sinc ...
- 转载Code First Migrations更新数据库架构的具体步骤
[转载] Code First Migrations更新数据库结构的具体步骤 我对 CodeFirst 的理解,与之对应的有 ModelFirst与 DatabaseFirst ,三者各有千秋,依项 ...
- 使用Code first 进行更新数据库结构(数据迁移)
CodeFirst 背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会 ...
- (转)使用Migrations更新数据库结构(Code First )
原文地址:http://blog.csdn.net/luoyeyu1989/article/details/8275237 背景 code first起初当修改model后,要持久化至数据库中时,总要 ...
- Code First 下自动更新数据库结构(Automatic Migrations)
示例 Web.config <?xml version="1.0" encoding="utf-8"?> <configuration> ...
- Code First 更新数据库结构(简单实现方法:会删除原来的数据)
之前在 http://www.cnblogs.com/mmcmmc/p/3833265.html 写到关于“Code First 更新数据库结构”的东西. 可是由于某种原因,新手们会出现各种问题,好了 ...
- Entity Framework 6 Code First的简单使用和更新数据库结构
一.安装Entity Framework 6 在项目中右击选择“管理NuGet程序包",联机搜索Entity Framework,点击安装 二.配置数据库连接 在App.config中加入数 ...
随机推荐
- django使用orm方式查询mogodb的某段时间的值
在使用djgango时,需要在数据表中过滤出在某段时间的内容,网上很多或者说Django的orm是针对mysql,且字段类型是datetime或者其他时间类型,使用__rang这个函数就可以查询某个时 ...
- bzoj2111 [ZJOI2010]排列计数
Description 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic ...
- swift语言的特点(相对于oc)
1.泛型.泛型约束与扩展: 2.函数式编程: 3.值类型.引用类型: 4.枚举.关联值.元组等其他 上述为swift最大的特点 Another safety feature is that by de ...
- 塔防cocos2d
塔防游戏,类似于保卫萝卜的一种. 需要注意的是几点问题是: 游戏地图是瓦片地图,设置特定的标记,用来标记哪些点是地图点,哪些是塔点. 游戏关卡选择:需要在两个cpp文件传参,用的是静态成员变量. 每一 ...
- ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(七)之 图文,附件消息(2016-05-05 12:13)
上一篇介绍了加好友的流程,这里不再赘述,不过之前的聊天只能发送普通文字,那么本篇就教你如何实现发送附件和图片消息.我们先对功能进行分析: 发送图片,附件,需要实现上传图片和附件的功能. textare ...
- cblas_sgemm cblas.h
BLAS(Basic Linear Algebra Subprograms)库,是用Fortran语言实现的向量和矩阵运算库,是许多数值计算软件库的核心, 但也有一些其它的包装, 如cblas是C语言 ...
- JavaScript数组实战小练习
1.找出元素在数组中的位置. function indexOf(arr, item) { if(Array.prototype.indexOf){ //判断浏览器是否支持indexOf方法 retur ...
- win10 安装YII2
YII2下载地址:http://www.yiichina.com/download 高级版本和基本版本的区别是: 基础版只有一个只有一个web应用,高级版则生成前后台.建议使用高级版,方便 Yii2框 ...
- linux系统命令与常识
之前短期学过linux,用到时才发现已经忘得一干二净了. 现在对学过的和了解到的做一个总结: 先明确一些使用工具: winscp : WinSCP是一个Windows环境下使用SSH的开源图形化SFT ...
- Django-rest-framework(二)serializers 使用
简介 初次见到serializers文件,想必大家都会感到陌生,所以,我们不妨换个词来形容他的作用,那就是django 中的Form,这样是不是感觉熟悉了一点. 实际上,serializers 的作用 ...