qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          CodeFirstMigrations更新數(shù)據(jù)庫結(jié)構(gòu)

            背景
            code first起初當(dāng)修改model后,要持久化至數(shù)據(jù)庫中時,總要把原數(shù)據(jù)庫給刪除掉再創(chuàng)建(DropCreateDatabaseIfModelChanges),此時就會產(chǎn)生一個問題,當(dāng)我們的舊數(shù)據(jù)庫中包含一些測試數(shù)據(jù)時,當(dāng)持久化更新后,原數(shù)據(jù)將全部丟失,故我們可以引入EF的數(shù)據(jù)遷移功能來完成。
            要求
            已安裝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;}
          }
            注:區(qū)別在于,我們給teacherName屬性加了一個長度限制。
            接下來,我們將開始持久化此model至數(shù)據(jù)庫中(我們現(xiàn)在只是對屬性作修改,此時數(shù)據(jù)庫中此字段的長度為nvarchar(max),并不是nvarchar(10))
            1:在config中配置數(shù)據(jù)庫連接:
            <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:運(yùn)行命令Enable-Migrations
            可能會出現(xiàn)如下錯誤:
            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.
            此時項目會出現(xiàn)如下文件夾:
            打開configuation.cs,將作出如下修改:
            public Configuration()
            {
            AutomaticMigrationsEnabled = true;
            }
            再次執(zhí)行Update-Database:
            因?yàn)槲野验L度從max改為10,在更新數(shù)據(jù)結(jié)構(gòu)時,它認(rèn)為此操作會導(dǎo)致數(shù)據(jù)丟失,如下:
            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.
            如果確保沒事,只需給此命令加個強(qiáng)制執(zhí)行的參數(shù)即可:
            Enable-Migrations -Force
            最后再次執(zhí)行:Update-Database
            數(shù)據(jù)庫中的原數(shù)據(jù)也沒有丟失!

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

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

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 金平| 醴陵市| 太和县| 偏关县| 南昌县| 普陀区| 伊春市| 宣威市| 罗城| 芜湖县| 苍溪县| 泉州市| 钦州市| 和田县| 新巴尔虎右旗| 太仆寺旗| 吉首市| 西林县| 韶关市| 威海市| 漳平市| 朝阳市| 墨玉县| 恩施市| 绥中县| 盐津县| 成武县| 房山区| 湄潭县| 鹰潭市| 运城市| 仪陇县| 江阴市| 萨迦县| 镶黄旗| 淮滨县| 资源县| 温泉县| 新河县| 呼玛县| 益阳市|