(转)使用Migrations更新数据库结构(Code First )
原文地址:http://blog.csdn.net/luoyeyu1989/article/details/8275237
code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的旧数据库中包含一些测试数据时,当持久化更新后,原数据将全部丢失,故我们可以引入EF的数据迁移功能来完成。
- 已安装NuGet
//原model
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class Lesson {
public int lessonID { get; set; }
[Required]
[MaxLength()]
public string lessonName { get; set; }
[Required]
public string teacherName { get; set; }
public virtual UserInfo UserInfo{get;set;}
}
//新model
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class Lesson {
public int lessonID { get; set; }
[Required]
[MaxLength()]
public string lessonName { get; set; }
[Required]
[MaxLength()]
public string teacherName { get; set; }
public virtual UserInfo UserInfo{get;set;}
}
接下来,我们将开始持久化此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>
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;
}
再次执行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

数据库中的原数据也没有丢失!
(转)使用Migrations更新数据库结构(Code First )的更多相关文章
- 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更新数据库结构(数据迁移) 【转】
注意:一旦正常后,每次数据库有变化,做如下两步: 1. Enable-Migrations 2.update-database 背景 code first起初当修改model后,要持久化至数据库中时, ...
- 转载Code First Migrations更新数据库架构的具体步骤
[转载] Code First Migrations更新数据库结构的具体步骤 我对 CodeFirst 的理解,与之对应的有 ModelFirst与 DatabaseFirst ,三者各有千秋,依项 ...
- Code First 下自动更新数据库结构(Automatic Migrations)
示例 Web.config <?xml version="1.0" encoding="utf-8"?> <configuration> ...
- Entity Framework 6 Code First的简单使用和更新数据库结构
一.安装Entity Framework 6 在项目中右击选择“管理NuGet程序包",联机搜索Entity Framework,点击安装 二.配置数据库连接 在App.config中加入数 ...
- Code First 更新数据库结构(简单实现方法:会删除原来的数据)
之前在 http://www.cnblogs.com/mmcmmc/p/3833265.html 写到关于“Code First 更新数据库结构”的东西. 可是由于某种原因,新手们会出现各种问题,好了 ...
- EF core (code first) 通过自定义 Migration History 实现多租户使用同一数据库时更新数据库结构
前言 写这篇文章的原因,其实由于我写EF core 实现多租户的时候,遇到的问题. 具体文章的链接: Asp.net core下利用EF core实现从数据实现多租户(1) Asp.net core下 ...
随机推荐
- SQL Server 2012 performance dashboard 安装
微软提供了一个很好用的工具performance dashboard: 下载地址: http://www.microsoft.com/en-us/download/details.aspx?id=29 ...
- div容器内文本对齐--神奇的css
有时候使用一些css往往能达到意想不到的效果 最近需要在页面上显示读取的文本内容,中英文混杂着,我把它们统统抛到div中div设置了宽度,效果是相当糟糕,左对齐,右端长短不一,有的超出长度,有的不够长 ...
- 机器学习之SVM(支持向量机)
支持向量机(SVM)是当前非常流行的监督学习方法,其核心主要有两个: 构造一个极大边距分离器--与样例点具有最大可能距离的决策边界: 将在原输入空间中线性不可分的样例映射到高维空间中,从而进行线性分离 ...
- LeetCode 75
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- [设计模式]<<设计模式之禅>>关于依赖倒置原则
依赖倒置原则(Dependence Inversion Principle,DIP)这个名字看着有点别扭,“依赖”还“倒置”,这到底是什么意思?依赖倒置原则的原始定义是 High level modu ...
- nmap命令-----基础用法
系统漏洞扫描之王-nmap NMap,也就是Network Mapper,是Linux下的网络扫描和嗅探工具包. 其基本功能有三个: (1)是扫描主机端口,嗅探所提供的网络服务 (2)是探测一 ...
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render();
只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called durin ...
- Java解析XMl文件之SAX和DOm方法
如题,这两种方法的jar包都在jdk中,不需要下载. 先来说下目录结构: 首先建一个Peron类封装person.xml的属性:DomParseService和SaxParseService分别为两种 ...
- 【转载】Kafka High Availability
http://www.haokoo.com/internet/2877400.html Kafka在0.8以前的版本中,并不提供High Availablity机制,一旦一个或多个Broker宕机,则 ...
- ComboBoxEdit数据绑定
ComboBoxEdit也是DevExpress winform控件中经常使用的一个,我们在使用的过程中可能有时需要对ComboBoxEdit控件进行数据绑定,而ComboBoxEdit控件不像 Lo ...