參考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table數據庫
本文內容:mvc
1,學習Entity Framework Code First 遷移功能(Migrations)asp.net
2,更新Model Class(Model Class中添加新的字段),而後把更新應用於datebase.ide
默認狀況下,就像咱們以前用Models下的movie.cs右鍵點擊直接添加database的時候,Code First自動在數據庫中添加新的表來記錄新添加的database時候和movie.cs同步,若是不一樣步,Entity Framework就會跑出錯誤。這樣咱們就能夠在開發的過程當中發現錯誤,而不是必須在運行的時候才發現錯誤。學習
一,爲Model的改變設置Code First Migrations功能(Setting up Code First Migrations for Model Changes),從而可讓movie.cs改變的時候,把改變應用於Database(Model類和自動生成的數據庫同步)
測試
1,1刪除MovieDBContext,刪除.mdf文件spa
![](http://static.javashuo.com/static/loading.gif)
1.2Ctrl+Shift+B重建解決方案後,打開「套件管理器控制檯」:.net
![](http://static.javashuo.com/static/loading.gif)
PM>提示符後面執行Enable-Migrations命令開啓Code First Migrations功能,咱們如今直接輸入:Enable-Migrations,能夠看到提示命令:
3d
咱們須要Enable的是Movie,直接Copy提示命令中的下面的一行命令:code
Enable-Migrations -ContextTypeName MvcMovie.Models.MovieDBContext
開啓成功:
![](http://static.javashuo.com/static/loading.gif)
解決方案下建立了Migrations文件夾:
![](http://static.javashuo.com/static/loading.gif)
1.3,更改Configuration.cs中的寫Seed方法,打開Configuration.cs,咱們看到
protected
override
void Seed(MVCMovie.Models.MovieDBContext context)
{
//
這個方法將會在Migrations後被調用
//
This method will be called after migrating to the latest version.
//
你能夠用DbSet<T>的.AddOrUpdate擴展方法,避免建立重複的數據
//
You can use the DbSet<T>.AddOrUpdate() helper extension method
//
to avoid creating duplicate seed data. E.g.
//
//
context.People.AddOrUpdate(
//
p => p.FullName,
//
new Person { FullName = "Andrew Peters" },
//
new Person { FullName = "Brice Lambson" },
//
new Person { FullName = "Rowan Miller" }
//
);
//
}
View Code
更新Seed方法:
protected
override
void Seed(MvcMovie.Models.MovieDBContext context)
{
context.Movies.AddOrUpdate( i => i.Title,
new Movie
{
Title =
"
When Harry Met Sally
",
ReleaseDate = DateTime.Parse(
"
1989-1-11
"),
Genre =
"
Romantic Comedy
",
Price =
7.99M
},
new Movie
{
Title =
"
Ghostbusters
",
ReleaseDate = DateTime.Parse(
"
1984-3-13
"),
Genre =
"
Comedy
",
Price =
8.99M
},
new Movie
{
Title =
"
Ghostbusters 2
",
ReleaseDate = DateTime.Parse(
"
1986-2-23
"),
Genre =
"
Comedy
",
Price =
9.99M
},
new Movie
{
Title =
"
Rio Bravo
",
ReleaseDate = DateTime.Parse(
"
1959-4-15
"),
Genre =
"
Western
",
Price =
3.99M
}
);
}
View Code
添加引用:
![](http://static.javashuo.com/static/loading.gif)
Code First Migrations 在每一次migration以後都會調用這個Seed方法,從而更新Database中的數據(Insert orUpdate)
1,4重建解決方案。爲Migrations建立一個新的DbMigration
.cs
類繼承自DbMigration
,這一步將會新建一個Database,這就是咱們以前要刪除movie.mdf
的緣由.
在Package Manager Console中執行命令:add-migration Initial
生成 intial migration. 「intial」是任意命名的,用來命名建立的migration文件。這個類是用來建立新的數據庫
![](http://static.javashuo.com/static/loading.gif)
Code First 建立了一個帶有時間戳的_Initial.cs,這個類中的代碼實現了建立數據庫表,當你更新類中的表的時候,_Initial.cs將會運行更新dabatbase中的表,而後Seed方法填充測試數據到Database的Table中。這些遷移文件類文件都是以時間戳爲前綴命名並排序的:
![](http://static.javashuo.com/static/loading.gif)
public
partial
class Initial : DbMigration
{
public
override
void Up()
{
CreateTable(
"
dbo.Movies
",
c =>
new
{
ID = c.Int(nullable:
false, identity:
true),
Title = c.String(),
ReleaseDate = c.DateTime(nullable:
false),
Genre = c.String(),
Price = c.Decimal(nullable:
false, precision:
18, scale:
2),
})
.PrimaryKey(t => t.ID);
}
public
override
void Down()
{
DropTable(
"
dbo.Movies
");
}
}
View Code
如今咱們運行命令PM>update-datebase 來建立數據庫,運行Seed方法:
![](http://static.javashuo.com/static/loading.gif)
若是運行update-database方法的時提示Table已經存在,是由於你在刪除表以後運行了項目。若是是這樣再次把Movie.mdf刪除,而後執行update-datebase命令。若是仍是報錯,刪除Migrations文件夾,而後從上面刪除Movie.mdf處從新按照本文介紹的一步一步來。
Ctrl+F5執行程式,咱們看到了Seed方法中的數據:
![](http://static.javashuo.com/static/loading.gif)
二,在Movie Model中添加新的屬性字段,把字段同步到DB的Table中
namespace MVCMovie.Models
{
//
Moive 類就至關於數據庫中的一張名爲Movie的Table
//
Movie 類實例化的對象至關於Table中的一行,實例的各個屬性(ID,Title...)至關於Table中的列
public
class Movie
{
public
int ID {
get;
set; }
public
string Title {
get;
set; }
public DateTime ReleaseDate {
get;
set; }
public
string Genre {
get;
set; }
public
decimal Price {
get;
set; }
public
string Rating {
get;
set; }
}
//
MovieDBContext class ,繼承自Entity Framework中的DbContext,表明這Movie數據上下文
//
MovieDBContext class ,讀取、存儲、更新Movie Class 實例
public
class MovieDBContext : DbContext
{
public DbSet<Movie> Movies {
get;
set; }
}
}
View Code
Ctrl+Shift+B重建解決方案,而後在View中的各個頁面添加Rating屬性:
![](http://static.javashuo.com/static/loading.gif)
<div
class=
"
editor-label
">
@Html.LabelFor(model => model.Rating)
</div>
<div
class=
"
editor-field
">
@Html.EditorFor(model => model.Rating)
@Html.ValidationMessageFor(model => model.Rating)
</div>
View Code
Ctrl+F5運行程序,提示以下:
![](http://static.javashuo.com/static/loading.gif)
這是由於咱們剛剛在Model中的Movie.cs中添加了新的屬性字段,Movie.cs中的字段和已經存在的Database中的表字段不一致:
![](http://static.javashuo.com/static/loading.gif)
咱們用Code First Migrations來解決這個問題:
1,在Seed中的每個對象實例中添加 Rating= "G",如:
context.Movies.AddOrUpdate(i => i.Title,
new Movie
{
Title =
"
When Harry Met Sally
",
ReleaseDate = DateTime.Parse(
"
1989-1-11
"),
Genre =
"
Romantic Comedy
",
Rating =
"
G
",
Price =
7.99M
},
View Code
2,
執行如下命令PM>add-migration AddRatingMig
這個命令告訴migration framework 檢查當前的movie model是否和dabatase 中的Movie 字段一致,若是不一致,就會添加必要的code來更新DB和新的Model一致:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
3,解決方案下面新建了帶有時間戳的_AddRatingMig.cs文件,文件中有添加和刪除新的列,保證Model和DB
中的字段一致
namespace MVCMovie.Migrations
{
using System;
using System.Data.Entity.Migrations;
public
partial
class AddRatingMig : DbMigration
{
public
override
void Up()
{
AddColumn(
"
dbo.Movies
",
"
Rating
", c => c.String());
}
public
override
void Down()
{
DropColumn(
"
dbo.Movies
",
"
Rating
");
}
}
}
View Code
4,執行命令PM>update-database
5,刷新」MovieDBContext」後咱們看到在Movie.cs中添加的屬性同步到了DB的Table中:
![](http://static.javashuo.com/static/loading.gif)
Migrations Configuration.cs中的Sample數據也填充到了Database中:
![](http://static.javashuo.com/static/loading.gif)
6,Ctrl+Shift+B,Ctrl+F5運行,咱們看到全部的頁面中都有了Rating:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
這一節,咱們能夠在Model中添加新的屬性,而且同步到DB中。咱們也學習了,把Sample數據填充到DB的Table中。下一節,咱們將會在Model Class中對新增數據添加邏輯驗證(validation logic)和業務規則(business rules).
See You。。。