qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          CodeFirstMigrations更新數據庫結構

            背景
            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(50)]
          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(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>
            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
            數據庫中的原數據也沒有丟失!

          posted on 2014-07-21 09:55 順其自然EVO 閱讀(343) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 棋牌| 高淳县| 大港区| 新密市| 太仓市| 万年县| 景洪市| 英吉沙县| 桐乡市| 汝城县| 全椒县| 河东区| 潮州市| 集安市| 龙游县| 泌阳县| 上高县| 乐平市| 遂宁市| 开阳县| 山西省| 渝北区| 揭阳市| 苏尼特左旗| 安岳县| 清丰县| 鄱阳县| 军事| 扶风县| 舒城县| 黄龙县| 昌图县| 阿瓦提县| 娄底市| 玉树县| 稷山县| 武穴市| 余庆县| 江达县| 蓝田县| 台湾省|