zoukankan      html  css  js  c++  java
  • Code First Migrations更新数据库结构(数据迁移) 【转】

    注意:一旦正常后,每次数据库有变化,做如下两步:

    1. Enable-Migrations

    2.update-database

    背景

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

    要求
    1. 已安装NuGet
    过程示例
    1. //原model  
    //原model
    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;}  
    12. }  
    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;}
    }
    1. //新model  
    //新model
    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;}  
    13. }  
    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中配置数据库连接:

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

    1. public Configuration()  
    2. {  
    3.     AutomaticMigrationsEnabled = true;  
    4. }  
            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

  • 相关阅读:
    微信小程序常用表单校验方法(手机号校验、身份证号(严格和非严格校验、验证码六位数字校验))
    css使Img图片居中显示
    formData请求接口传递参数格式
    台式机如何无线上网的解决方法
    (转译)2019年WEB漏洞扫描工具和软件前十名推荐
    如何往虚拟机内传文件的3种方法
    Python3.X Selenium 自动化测试中如何截图并保存成功
    postman如何绕过登录账户和密码验证,进行接口测试的方法
    官网Windows 10安装程序驱动下载--截止:2019.01.06版本
    postman教学视频百度网盘转载分享
  • 原文地址:https://www.cnblogs.com/KKSoft/p/4824615.html
Copyright © 2011-2022 走看看