背景

code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的旧数据库中包含一些测试数据时,当持久化更新后,原数据将全部丢失,故我们可以引入EF的数据迁移功能来完成。

要求

  1. 已安装NuGet

过程示例

[csharp] view plaincopy

  1. //原model

[csharp] view plaincopy

  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. public class Lesson {  
  5. public int lessonID { get; set; }  
  6.     [Required]  
  7.     [MaxLength(50)]  
  8. public string lessonName { get; set; }  
  9.     [Required]  
  10. public string teacherName { get; set; }  
  11. public virtual UserInfo UserInfo{get;set;}  

[csharp] view plaincopy

  1. //新model

[csharp] view plaincopy

  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. public class Lesson {  
  5. public int lessonID { get; set; }  
  6.     [Required]  
  7.     [MaxLength(50)]  
  8. public string lessonName { get; set; }  
  9.     [Required]  
  10.     [MaxLength(10)]  
  11. public string teacherName { get; set; }  
  12. public virtual UserInfo UserInfo{get;set;}  

注:区别在于,我们给teacherName属性加了一个长度限制。

接下来,我们将开始持久化此model至数据库中(我们现在只是对属性作修改,此时数据库中此字段的长度为nvarchar(max),并不是nvarchar(10))

1:在config中配置数据库连接:

[html] view plaincopy

  1. <connectionStrings>
  2. <add name="TestUsersDB" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestUsersDB;Data Source=XCL-PC\SQLEXPRESS" providerName="System.Data.SqlClient" />
  3. </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,将作出如下修改:

[csharp] view plaincopy

  1. public Configuration()  
  2. {  
  3.     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

数据库中的原数据也没有丢失!

CodeFirstMigrations更新数据库结构(EF数据迁移)的更多相关文章

  1. ASP.NET MVC4 新手入门教程特别篇之一----Code First Migrations更新数据库结构(数据迁移)修改Entity FrameWork 数据结构(不删除数据)

    背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的 ...

  2. Code First Migrations更新数据库结构(数据迁移)

    背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建 (DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们 ...

  3. 使用Code first 进行更新数据库结构(数据迁移)

    CodeFirst 背景  code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会 ...

  4. Code First Migrations更新数据库结构(数据迁移) 【转】

    注意:一旦正常后,每次数据库有变化,做如下两步: 1. Enable-Migrations 2.update-database 背景 code first起初当修改model后,要持久化至数据库中时, ...

  5. Code First 更新数据库结构(简单实现方法:会删除原来的数据)

    之前在 http://www.cnblogs.com/mmcmmc/p/3833265.html 写到关于“Code First 更新数据库结构”的东西. 可是由于某种原因,新手们会出现各种问题,好了 ...

  6. 第三篇:SpringBoot - 数据库结构版本管理与迁移

    SpringBoot支持了两种数据库结构版本管理与迁移,一个是flyway,一个是liquibase.其本身也支持sql script,在初始化数据源之后执行指定的脚本,本章是基于 Liquibase ...

  7. Code First 下自动更新数据库结构(Automatic Migrations)

    示例 Web.config <?xml version="1.0" encoding="utf-8"?> <configuration> ...

  8. Entity Framework 6 Code First的简单使用和更新数据库结构

    一.安装Entity Framework 6 在项目中右击选择“管理NuGet程序包",联机搜索Entity Framework,点击安装 二.配置数据库连接 在App.config中加入数 ...

  9. SQLServer2008 和SQLServer2008 R2版本导出 数据库结构和数据sql

    ①SQLServer2008 版本导出 数据库结构和数据sql ②SQLServer2008R2 版本导出 数据库结构和数据sql 采集 #HUABAN_WIDGETS .HUABAN-red-nor ...

随机推荐

  1. C++数据结构之List--线性实现

    List(表)类似于队列,不同于队列的是,list可以随机读取/修改/插入某一position,通过position这一位置信息就可以直接修改相应位置的元素.实现方式和队列的类似,多了个positio ...

  2. Osmocom-BB多信道修改代码相关

    修改bb\src\target\firmware\layer1\prim_rx_nb.c 文件 这个nb表示normalburst,指的是ccch的数据,用专业的术语,应该是,一个ccch的burst ...

  3. BZOJ 1912 巡逻

    重赋边权. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...

  4. Cash Machine_多重背包

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  5. Java 集合深入理解(14):Map 概述

    点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 终于把 List 常用的几种容器介绍完了,接下来开始 Map 的相关介绍. 什么是 Map Java 中的 Map 接口 ...

  6. 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. dede的pagelist标签的listsize数字属性详解(借鉴)

    dede的pagelist标签的listsize数字属性详解.见远seo经常用织梦搭建各种网站,有次发现列表页面的分页显示超过div的界限,也就是溢出了或者说是撑破了.后来经过研究发现是pagelis ...

  8. 最小二乘法 python实现

    #-*-coding:UTF-8-*- # Created on 2015年10月20日 # @author: hanahimi import numpy as np import random im ...

  9. Linux中“新旧”TCP/IP工具的对比

    如今很多系统管理员依然通过组合使用诸如ifconfig.route.arp和netstat等命令行工具(统称为net-tools)来配置网络功能.解决网络故障,net-tools起源于BSD的TCP/ ...

  10. ZSDRM001-发货清单

    REPORT ZSDRM001 LINE-SIZE 225 LINE-COUNT 65 NO STANDARD PAGE HEADING.*------------------------------ ...