Setting up Code First Migrations for Model Changes--為模型更改做數(shù)據(jù)庫(kù)遷移。
1.打開(kāi)資源管理器,在App_Data文件夾下,找到movies.mdf數(shù)據(jù)庫(kù)文件,如果沒(méi)有看到點(diǎn)擊顯示所有文件。
2.刪掉movies.mdf數(shù)據(jù)庫(kù)文件,并編譯項(xiàng)目。確保沒(méi)有報(bào)錯(cuò)。
3.找到工具菜單欄下面的NuGet程序包管理器---程序包管理器控制臺(tái),如圖所示:
4,在程序包管理器控制臺(tái)中,輸入:Enable-Migrations -ContextTypeName MvcMovie.Models.MovieDBContext
(注意:MvcMovie.Models.MovieDBContext 項(xiàng)目名.Models.項(xiàng)目數(shù)據(jù)上下文)
按enter鍵之后,可以看到:
5,數(shù)據(jù)庫(kù)遷移之后,VS中自動(dòng)為我們生成了一個(gè)Migrations文件夾,里面有一個(gè)Configuration.cs文件
6.打開(kāi)Configuration.cs文件,引用命名空間:
using MvcMovie.Models;
然后在Seed方法中寫(xiě)上:
1 PRotected override void Seed(MvcMovie.Models.MovieDBContext context) 2 { 3 context.Movies.AddOrUpdate( i => i.Title, 4 new Movie 5 { 6 Title = "When Harry Met Sally", 7 ReleaseDate = DateTime.Parse("1989-1-11"), 8 Genre = "Romantic Comedy", 9 Price = 7.99M10 },11 12 new Movie13 {14 Title = "Ghostbusters ",15 ReleaseDate = DateTime.Parse("1984-3-13"),16 Genre = "Comedy",17 Price = 8.99M18 },19 20 new Movie21 {22 Title = "Ghostbusters 2",23 ReleaseDate = DateTime.Parse("1986-2-23"),24 Genre = "Comedy",25 Price = 9.99M26 },27 28 new Movie29 {30 Title = "Rio Bravo",31 ReleaseDate = DateTime.Parse("1959-4-15"),32 Genre = "Western",33 Price = 3.99M34 }35 );36 37 }
7.Code First Migrations calls theSeed
method after every migration (that is, callingupdate-databasein the Package Manager Console), and this method updates rows that have already been inserted, or inserts them if they don't exist yet.
這句話的意思是:在每一次數(shù)據(jù)庫(kù)遷移的時(shí)候,這個(gè)seed方法都會(huì)被調(diào)用,這個(gè)方法更新已經(jīng)插入的行數(shù)據(jù),或者插入行,如果這個(gè)行數(shù)據(jù)不存在。
8.下面的方法起到了一個(gè)更新插入的作用:
1 context.Movies.AddOrUpdate(i => i.Title,2 new Movie3 {4 Title = "When Harry Met Sally",5 ReleaseDate = DateTime.Parse("1989-1-11"),6 Genre = "Romantic Comedy",7 Rating = "PG",8 Price = 7.99M9 }
9.*因?yàn)镾eed方法,在每次數(shù)據(jù)庫(kù)遷移的時(shí)候,都會(huì)執(zhí)行。你不能僅僅是插入數(shù)據(jù),因?yàn)槟銓⒁迦氲臄?shù)據(jù),將在第一次數(shù)據(jù)庫(kù)遷移結(jié)束之后,已經(jīng)存在數(shù)據(jù)庫(kù)中;
*更新插入的操作可以預(yù)防錯(cuò)誤,通過(guò)阻止你,插入已經(jīng)存在的數(shù)據(jù)到數(shù)據(jù)庫(kù)中。但是它有個(gè)缺點(diǎn):它重載了,所有你在測(cè)試項(xiàng)目時(shí)候改變的數(shù)據(jù);
因?yàn)橛行y(cè)試數(shù)據(jù),你不想改變:比如,你測(cè)試的時(shí)候,改變了數(shù)據(jù),但是你不想這個(gè)數(shù)據(jù)在數(shù)據(jù)庫(kù)更新的時(shí)候,發(fā)生改變。這個(gè)時(shí)候你可以做一個(gè)新增的操作:新增一個(gè)數(shù)據(jù)庫(kù)中不存在的數(shù)據(jù)。
10.看一下這句代碼吧:context.Movies.AddOrUpdate(i => i.Title,這第一個(gè)傳到AddOrUpdate方法的參數(shù),指定了一個(gè)屬性,用來(lái)檢查是否已經(jīng)存在相同的行數(shù)據(jù),對(duì)于我這個(gè)項(xiàng)目來(lái)說(shuō),我這個(gè)Title,可以做為這個(gè)屬性,因?yàn)樗贚ist中每次都是唯一的;This code assumes that titiles are unique. If you manually add a duplicate title, you'll get the following exception the next time you perform a migration.
Sequence contains more than one element 我們假想Title是唯一的,如果你手動(dòng)添加了重復(fù)的Title,你將會(huì)在下次數(shù)據(jù)庫(kù)遷移的時(shí)候,報(bào)一個(gè)錯(cuò)誤,了解更多,請(qǐng)參考鏈接的文章,哈哈,純?nèi)斯?a href="http://www.survivalescaperooms.com/tools/zaixianfanyi.asp">翻譯的哦。因?yàn)椴┲飨矚g英語(yǔ),所以還是看外國(guó)人的資料學(xué)習(xí)編程了。MSDN很不錯(cuò)的,For more information about theAddOrUpdatemethod, seeTake care with EF 4.3 AddOrUpdate Method..11.現(xiàn)在我們來(lái)編譯一下整個(gè)項(xiàng)目吧,如果這這里不編譯的話,后面的步驟中將會(huì)出錯(cuò)誤。12.The next step is to create aDbMigration
class for the initial migration. This migration creates a new database, that's why you deleted themovie.mdffile in a previous step.這句話的意思是:我們接下來(lái)要為初始化數(shù)據(jù)庫(kù)遷移,創(chuàng)建一個(gè)DBMigration類(lèi),這個(gè)數(shù)據(jù)庫(kù)遷移創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù),這也就是我們前面刪掉Movie.mdf文件的原因。13.在程序包管理器控制臺(tái)中輸入:add-migration Initial我們看到:
14.Code First Migrations creates another class file in theMigrationsfolder (with the name{DateStamp}_Initial.cs), and this class contains code that creates the database schema. The migration filename is pre-fixed with a timestamp to help with ordering. Examine the{DateStamp}_Initial.csfile, it contains the instructions to create theMovies
table for the Movie DB. When you update the database in the instructions below, this{DateStamp}_Initial.csfile will run and create the the DB schema. Then theSeedmethod will run to populate the DB with test data.
這段話的意思是:Code First遷移,在Migration文件下,創(chuàng)建了另外一個(gè)類(lèi)(類(lèi)的文件名稱是:時(shí)間_Initiaal.cs),并且這個(gè)類(lèi)包含了創(chuàng)建數(shù)據(jù)庫(kù)的代碼。這個(gè)文件以時(shí)間的命名方式便于排序管理。檢查這個(gè)文件,它包含了怎么為MovieDB創(chuàng)建Moviess數(shù)據(jù)庫(kù)表。當(dāng)你按照下面的指令(等會(huì)我在控制器管理控制臺(tái)中輸入的指定),更新數(shù)據(jù)庫(kù)的時(shí)候,這個(gè)文件會(huì)執(zhí)行,并且創(chuàng)建數(shù)據(jù)庫(kù),然后這個(gè)Seed方法,也將會(huì)執(zhí)行,為數(shù)據(jù)庫(kù)生成測(cè)試數(shù)據(jù)。
先看看看這個(gè)文件里面的代碼是啥樣的吧:
1 namespace MvcMovie.Migrations 2 { 3 using System; 4 using System.Data.Entity.Migrations; 5 6 public partial class Initial : DbMigration 7 { 8 public override void Up() 9 {10 CreateTable(11 "dbo.Movies",12 c => new13 {14 ID = c.Int(nullable: false, identity: true),15 Title = c.String(),16 ReleaseDate = c.DateTime(nullable: false),17 Genre = c.String(),18 Price = c.Decimal(nullable: false, precision: 18, scale: 2),19 })20 .PrimaryKey(t => t.ID);21 22 }23 24 public override void Down()25 {26 DropTable("dbo.Movies");27 }28 }29 }
同樣看看我們之前的Migration里面Configuration.cs代碼吧:
1 namespace MvcMovie.Migrations 2 { 3 using MvcMovie.Models; 4 using System; 5 using System.Data.Entity; 6 using System.Data.Entity.Migrations; 7 using System.Linq; 8 9 internal sealed class Configuration : DbMigrationsConfiguration<MvcMovie.Models.MovieDBContext>10 {11 public Configuration()12 {13 AutomaticMigrationsEnabled = false;14 }15 16 protected override void Seed(MvcMovie.Models.MovieDBContext context)17 {18 context.Movies.AddOrUpdate(i => i.Title,19 new Movie20 {21 Title = "When Harry Met Sally",22 ReleaseDate = DateTime.Parse("1989-1-11"),23 Genre = "Romantic Comedy",24 Price = 7.99M25 },26 27 new Movie28 {29 Title = "Ghostbusters ",30 ReleaseDate = DateTime.Parse("1984-3-13"),31 Genre = "Comedy",32 Price = 8.99M33 },34 35 new Movie36 {37 Title = "Ghostbusters 2",38 ReleaseDate = DateTime.Parse("1986-2-23"),39 Genre = "Comedy",40 Price = 9.99M41 },42 43 new Movie44 {45 Title = "Rio Bravo",46 ReleaseDate = DateTime.Parse("1959-4-15"),47 Genre = "Western",48 Price = 3.99M49 }50 );51 }52 }53 }
現(xiàn)在我們?cè)冢绦虬芾砥骺刂婆_(tái)中輸入這個(gè)指令來(lái)創(chuàng)建數(shù)據(jù)庫(kù),并運(yùn)行seed方法:
update-database
我們可以看到:
If you get an error that indicates a table already exists and can't be created, it is probably because you ran the application after you deleted the database and before you executedupdate-database
. In that case, delete theMovies.mdffile again and retry theupdate-database
command. If you still get an error, delete the migrations folder and contents
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注