文件
-
Razor Pages 上教學課程系列的第 5 部分。
-
Razor 頁面上教學課程系列的第 6 部分。
-
Razor Pages 上教學課程系列的第 4 部分。
注意
這不是這篇文章的最新版本。 如需目前的版本,請參閱 本文的 .NET 9 版本。
警告
不再支援此版本的 ASP.NET Core。 如需詳細資訊,請參閱 .NET 和 .NET Core 支持原則。 如需目前的版本,請參閱 本文的 .NET 9 版本。
在本節 中,Entity Framework Core (EF Core) 用來根據應用程式的模型類別定義資料庫架構:
此方法 EF Core 允許更敏捷的開發程式。 開發人員直接在應用程式的數據模型上工作,同時建立並同步資料庫架構,而無需開發人員在資料庫管理工具中切換上下文。 如需 Entity Framework Core 的概觀及其優點,請參閱 Entity Framework Core。
使用 EF Code 自動建立及追蹤資料庫:
__EFMigrationsHistory
資料表新增至資料庫,以追蹤資料庫的結構描述是否與其產生的來源模型類別同步。自動驗證結構描述和模型同步,可讓您更輕鬆地找出不一致的資料庫程式碼問題。
開啟 Models/Movie.cs
檔案,並新增 Rating
屬性:
public class Movie
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; } = string.Empty;
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
public string Rating { get; set; } = string.Empty;
}
編輯 Pages/Movies/Index.cshtml
,並新增 Rating
欄位:
@page
@model RazorPagesMovie.Pages.Movies.IndexModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<form>
<p>
<select asp-for="MovieGenre" asp-items="Model.Genres">
<option value="">All</option>
</select>
<label>Title: <input type="text" asp-for="SearchString" /></label>
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Rating)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Movie)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
使用 Rating
欄位更新下列頁面:
在更新資料庫以包含新欄位之前,應用程式無法運作。 在未更新資料庫的情況下執行應用程式,會擲回 SqlException
:
SqlException: Invalid column name 'Rating'.
此 SqlException
例外狀況是因為更新的電影模型類別,不同於資料庫之電影資料表的結構描述。 資料庫資料表中沒有任何 Rating
資料行。
有幾個方法可以解決這個錯誤:
在本教學課程中,請使用 EF Core 移轉。
更新 SeedData
類別,使其提供新資料行的值。 範例變更如下所示,但每個 new Movie
區塊都要進行這項變更。
context.Movie.AddRange(
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M,
Rating = "R"
},
請參閱完整的 SeedData.cs 檔案 (英文)。
建置應用程式
按下 Ctrl+Shift+B
從 [工具] 功能表中,選取 [NuGet 套件管理員]> [套件管理員主控台]。
請在套件管理員主控台 (PMC) 中輸入下列命令:
Add-Migration Rating
Add-Migration
命令會告知架構,以便:
Movie
模型與 Movie
資料庫結構描述。"Rating" 是用來命名移轉檔案的任意名稱。 建議您針對移轉檔案使用有意義的名稱,更加實用。
在 PMC 中,輸入下列命令:
Update-Database
Update-Database
命令會指示架構將結構描述變更套用至資料庫,並保留現有的資料。
刪除資料庫中的所有記錄,初始設定式會將內容植入資料庫,並包含 Rating
欄位。 可以透過瀏覽器中的刪除連結或 Sql Server 物件總管 (SSOX) 進行刪除。
另一個選擇是刪除資料庫並使用移轉重新建立資料庫。 若要在 SSOX 中刪除資料庫:
在 SSOX 中選取資料庫。
以滑鼠右鍵按一下資料庫,然後選取 [刪除]。
核取 [關閉現有的連接]。
選取 [確定]。
在 PMC 中,更新資料庫:
Update-Database
執行應用程式,並驗證您可以使用 Rating
欄位建立、編輯和顯示電影。 若未植入資料庫,請在 SeedData.Initialize
方法中設定中斷點。
在本節中,您會使用 Entity Framework Code First 移轉:
使用 EF Code First 自動建立和追蹤資料庫時,Code First 會:
__EFMigrationsHistory
資料表新增至資料庫,以追蹤資料庫的結構描述是否與其產生的來源模型類別同步。自動驗證結構描述和模型同步,可讓您更輕鬆地找出不一致的資料庫程式碼問題。
開啟 Models/Movie.cs
檔案,並新增 Rating
屬性:
public class Movie
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; } = string.Empty;
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
public string Rating { get; set; } = string.Empty;
}
編輯 Pages/Movies/Index.cshtml
,並新增 Rating
欄位:
@page
@model RazorPagesMovie.Pages.Movies.IndexModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<form>
<p>
<select asp-for="MovieGenre" asp-items="Model.Genres">
<option value="">All</option>
</select>
<label>Title: <input type="text" asp-for="SearchString" /></label>
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Rating)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Movie)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
使用 Rating
欄位更新下列頁面:
在更新資料庫以包含新欄位之前,應用程式無法運作。 在未更新資料庫的情況下執行應用程式,會擲回 SqlException
:
SqlException: Invalid column name 'Rating'.
此 SqlException
例外狀況是因為更新的電影模型類別,不同於資料庫之電影資料表的結構描述。 資料庫資料表中沒有任何 Rating
資料行。
有幾個方法可以解決這個錯誤:
在本教學課程中,請使用 Code First 移轉。
更新 SeedData
類別,使其提供新資料行的值。 範例變更如下所示,但每個 new Movie
區塊都要進行這項變更。
context.Movie.AddRange(
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M,
Rating = "R"
},
請參閱完整的 SeedData.cs 檔案 (英文)。
建置應用程式
按下 Ctrl+Shift+B
從 [工具] 功能表中,選取 [NuGet 套件管理員]> [套件管理員主控台]。
在 PMC 中,輸入下列命令:
Add-Migration Rating
Update-Database
Add-Migration
命令會告知架構,以便:
Movie
模型與 Movie
資料庫結構描述。"Rating" 是用來命名移轉檔案的任意名稱。 建議您針對移轉檔案使用有意義的名稱,更加實用。
Update-Database
命令會指示架構將結構描述變更套用至資料庫,並保留現有的資料。
刪除資料庫中的所有記錄,初始設定式會將內容植入資料庫,並包含 Rating
欄位。 可以透過瀏覽器中的刪除連結或 Sql Server 物件總管 (SSOX) 進行刪除。
另一個選擇是刪除資料庫並使用移轉重新建立資料庫。 若要在 SSOX 中刪除資料庫:
在 SSOX 中選取資料庫。
以滑鼠右鍵按一下資料庫,然後選取 [刪除]。
核取 [關閉現有的連接]。
選取 [確定]。
在 PMC 中,更新資料庫:
Update-Database
執行應用程式,並驗證您可以使用 Rating
欄位建立、編輯和顯示電影。 若未植入資料庫,請在 SeedData.Initialize
方法中設定中斷點。
在本節中,您會使用 Entity Framework Code First 移轉:
使用 EF Code First 自動建立和追蹤資料庫時,Code First 會:
__EFMigrationsHistory
資料表新增至資料庫,以追蹤資料庫的結構描述是否與其產生的來源模型類別同步。自動驗證結構描述和模型同步,可讓您更輕鬆地找出不一致的資料庫程式碼問題。
開啟 Models/Movie.cs
檔案,並新增 Rating
屬性:
public class Movie
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; } = string.Empty;
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
public string Rating { get; set; } = string.Empty;
}
編輯 Pages/Movies/Index.cshtml
,並新增 Rating
欄位:
@page
@model RazorPagesMovie.Pages.Movies.IndexModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<form>
<p>
<select asp-for="MovieGenre" asp-items="Model.Genres">
<option value="">All</option>
</select>
<label>Title: <input type="text" asp-for="SearchString" /></label>
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Rating)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Movie)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
使用 Rating
欄位更新下列頁面:
在更新資料庫以包含新欄位之前,應用程式無法運作。 在未更新資料庫的情況下執行應用程式,會擲回 SqlException
:
SqlException: Invalid column name 'Rating'.
此 SqlException
例外狀況是因為更新的電影模型類別,不同於資料庫之電影資料表的結構描述。 資料庫資料表中沒有任何 Rating
資料行。
有幾個方法可以解決這個錯誤:
在本教學課程中,請使用 Code First 移轉。
更新 SeedData
類別,使其提供新資料行的值。 範例變更如下所示,但每個 new Movie
區塊都要進行這項變更。
context.Movie.AddRange(
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M,
Rating = "R"
},
請參閱完整的 SeedData.cs 檔案 (英文)。
建置應用程式
按下 Ctrl+Shift+B
從 [工具] 功能表中,選取 [NuGet 套件管理員]> [套件管理員主控台]。
在 PMC 中,輸入下列命令:
Add-Migration Rating
Update-Database
Add-Migration
命令會告知架構,以便:
Movie
模型與 Movie
資料庫結構描述。"Rating" 是用來命名移轉檔案的任意名稱。 建議您針對移轉檔案使用有意義的名稱,更加實用。
Update-Database
命令會指示架構將結構描述變更套用至資料庫,並保留現有的資料。
刪除資料庫中的所有記錄,初始設定式會將內容植入資料庫,並包含 Rating
欄位。 可以透過瀏覽器中的刪除連結或 Sql Server 物件總管 (SSOX) 進行刪除。
另一個選擇是刪除資料庫並使用移轉重新建立資料庫。 若要在 SSOX 中刪除資料庫:
在 SSOX 中選取資料庫。
以滑鼠右鍵按一下資料庫,然後選取 [刪除]。
核取 [關閉現有的連接]。
選取 [確定]。
在 PMC 中,更新資料庫:
Update-Database
執行應用程式,並驗證您可以使用 Rating
欄位建立、編輯和顯示電影。 若未植入資料庫,請在 SeedData.Initialize
方法中設定中斷點。
在本節中,您會使用 Entity Framework Code First 移轉:
使用 EF Code First 自動建立和追蹤資料庫時,Code First 會:
__EFMigrationsHistory
資料表新增至資料庫,以追蹤資料庫的結構描述是否與其產生的來源模型類別同步。自動驗證結構描述和模型同步,可讓您更輕鬆地找出不一致的資料庫程式碼問題。
開啟 Models/Movie.cs
檔案,並新增 Rating
屬性:
public class Movie
{
public int ID { get; set; }
public string Title { get; set; } = string.Empty;
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; } = string.Empty;
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
public string Rating { get; set; } = string.Empty;
}
編輯 Pages/Movies/Index.cshtml
,並新增 Rating
欄位:
@page
@model RazorPagesMovie.Pages.Movies.IndexModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<form>
<p>
<select asp-for="MovieGenre" asp-items="Model.Genres">
<option value="">All</option>
</select>
<label>Title: <input type="text" asp-for="SearchString" /></label>
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Rating)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Movie)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
使用 Rating
欄位更新下列頁面:
在更新資料庫以包含新欄位之前,應用程式無法運作。 在未更新資料庫的情況下執行應用程式,會擲回 SqlException
:
SqlException: Invalid column name 'Rating'.
此 SqlException
例外狀況是因為更新的電影模型類別,不同於資料庫之電影資料表的結構描述。 資料庫資料表中沒有任何 Rating
資料行。
有幾個方法可以解決這個錯誤:
在本教學課程中,請使用 Code First 移轉。
更新 SeedData
類別,使其提供新資料行的值。 範例變更如下所示,但每個 new Movie
區塊都要進行這項變更。
context.Movie.AddRange(
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M,
Rating = "R"
},
請參閱完整的 SeedData.cs 檔案 (英文)。
建置方案。
從 [工具] 功能表中,選取 [NuGet 套件管理員]> [套件管理員主控台]。
在 PMC 中,輸入下列命令:
Add-Migration Rating
Update-Database
Add-Migration
命令會告知架構,以便:
Movie
模型與 Movie
資料庫結構描述。"Rating" 是用來命名移轉檔案的任意名稱。 建議您針對移轉檔案使用有意義的名稱,更加實用。
Update-Database
命令會指示架構將結構描述變更套用至資料庫,並保留現有的資料。
刪除資料庫中的所有記錄,初始設定式會將內容植入資料庫,並包含 Rating
欄位。 可以透過瀏覽器中的刪除連結或 Sql Server 物件總管 (SSOX) 進行刪除。
另一個選擇是刪除資料庫並使用移轉重新建立資料庫。 若要在 SSOX 中刪除資料庫:
在 SSOX 中選取資料庫。
以滑鼠右鍵按一下資料庫,然後選取 [刪除]。
核取 [關閉現有的連接]。
選取 [確定]。
在 PMC 中,更新資料庫:
Update-Database
執行應用程式,並驗證您可以使用 Rating
欄位建立、編輯和顯示電影。 若未植入資料庫,請在 SeedData.Initialize
方法中設定中斷點。
檢視或下載範例程式碼 (如何下載)。
在本節中,您會使用 Entity Framework Code First 移轉:
使用 EF Code First 自動建立資料庫時,Code First 會:
__EFMigrationsHistory
資料表新增至資料庫,以追蹤資料庫的結構描述是否與其產生的來源模型類別同步。自動驗證結構描述和模型同步,可讓您更輕鬆地找出不一致的資料庫程式碼問題。
開啟 Models/Movie.cs
檔案,並新增 Rating
屬性:
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
public string Rating { get; set; }
}
建置應用程式。
編輯 Pages/Movies/Index.cshtml
,並新增 Rating
欄位:
@page
@model RazorPagesMovie.Pages.Movies.IndexModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<form>
<p>
<select asp-for="MovieGenre" asp-items="Model.Genres">
<option value="">All</option>
</select>
<label>Title: <input type="text" asp-for="SearchString" /></label>
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Rating)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Movie)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
使用 Rating
欄位更新下列頁面:
在更新資料庫以包含新欄位之前,應用程式無法運作。 在未更新資料庫的情況下執行應用程式,會擲回 SqlException
:
SqlException: Invalid column name 'Rating'.
此 SqlException
例外狀況是因為更新的電影模型類別,不同於資料庫之電影資料表的結構描述。 資料庫資料表中沒有任何 Rating
資料行。
有幾個方法可以解決這個錯誤:
讓 Entity Framework 自動卸除資料庫,並使用新的模型類別結構描述來重新建立資料庫。 在開發週期早期,這個方法會很方便,其可讓您一併調整模型和資料庫結構描述,更加快速。 缺點是會遺失在資料庫中的現有資料。 請勿在生產環境資料庫上使用此方法! 在結構描述變更時卸除資料庫以及使用初始設定式將測試資料自動植入資料庫,通常是開發應用程式的有效方式。
您可明確修改現有資料庫的結構描述,使其符合模型類別。 這種方法的優點是能夠保留資料。 手動方式或藉由建立資料庫變更指令碼來進行這項變更。
使用 Code First 移轉來更新資料庫結構描述。
在本教學課程中,請使用 Code First 移轉。
更新 SeedData
類別,使其提供新資料行的值。 範例變更如下所示,但每個 new Movie
區塊都要進行這項變更。
context.Movie.AddRange(
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M,
Rating = "R"
},
請參閱完整的 SeedData.cs 檔案 (英文)。
建置方案。
從 [工具] 功能表中,選取 [NuGet 套件管理員]> [套件管理員主控台]。
在 PMC 中,輸入下列命令:
Add-Migration Rating
Update-Database
Add-Migration
命令會告知架構,以便:
Movie
模型與 Movie
資料庫結構描述。"Rating" 是用來命名移轉檔案的任意名稱。 建議您針對移轉檔案使用有意義的名稱,更加實用。
Update-Database
命令會指示架構將結構描述變更套用至資料庫,並保留現有的資料。
刪除資料庫中的所有記錄,初始設定式會將內容植入資料庫,並包含 Rating
欄位。 可以透過瀏覽器中的刪除連結或 Sql Server 物件總管 (SSOX) 進行刪除。
另一個選擇是刪除資料庫並使用移轉重新建立資料庫。 若要在 SSOX 中刪除資料庫:
在 SSOX 中選取資料庫。
以滑鼠右鍵按一下資料庫,然後選取 [刪除]。
核取 [關閉現有的連接]。
選取 [確定]。
在 PMC 中,更新資料庫:
Update-Database
執行應用程式,並驗證您可以使用 Rating
欄位建立/編輯/顯示電影。 若未植入資料庫,請在 SeedData.Initialize
方法中設定中斷點。
檢視或下載範例程式碼 (如何下載)。
在本節中,您會使用 Entity Framework Code First 移轉:
使用 EF Code First 自動建立資料庫時,Code First 會:
__EFMigrationsHistory
資料表新增至資料庫,以追蹤資料庫的結構描述是否與其產生的來源模型類別同步。自動驗證結構描述和模型同步,可讓您更輕鬆地找出不一致的資料庫程式碼問題。
開啟 Models/Movie.cs
檔案,並新增 Rating
屬性:
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
[Column(TypeName = "decimal(18, 2)")]
public decimal Price { get; set; }
public string Rating { get; set; }
}
建置應用程式。
編輯 Pages/Movies/Index.cshtml
,並新增 Rating
欄位:
@page
@model RazorPagesMovie.Pages.Movies.IndexModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<form>
<p>
<select asp-for="MovieGenre" asp-items="Model.Genres">
<option value="">All</option>
</select>
<label>Title: <input type="text" asp-for="SearchString" /></label>
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Rating)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Movie)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
使用 Rating
欄位更新下列頁面:
在更新資料庫以包含新欄位之前,應用程式無法運作。 在未更新資料庫的情況下執行應用程式,會擲回 SqlException
:
SqlException: Invalid column name 'Rating'.
此 SqlException
例外狀況是因為更新的電影模型類別,不同於資料庫之電影資料表的結構描述。 資料庫資料表中沒有任何 Rating
資料行。
有幾個方法可以解決這個錯誤:
讓 Entity Framework 自動卸除資料庫,並使用新的模型類別結構描述來重新建立資料庫。 在開發週期早期,這個方法會很方便,其可讓您一併調整模型和資料庫結構描述,更加快速。 缺點是會遺失在資料庫中的現有資料。 請勿在生產環境資料庫上使用此方法! 在結構描述變更時卸除資料庫以及使用初始設定式將測試資料自動植入資料庫,通常是開發應用程式的有效方式。
您可明確修改現有資料庫的結構描述,使其符合模型類別。 這種方法的優點是能夠保留資料。 手動方式或藉由建立資料庫變更指令碼來進行這項變更。
使用 Code First 移轉來更新資料庫結構描述。
在本教學課程中,請使用 Code First 移轉。
更新 SeedData
類別,使其提供新資料行的值。 範例變更如下所示,但每個 new Movie
區塊都要進行這項變更。
context.Movie.AddRange(
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M,
Rating = "R"
},
請參閱完整的 SeedData.cs 檔案 (英文)。
建置方案。
從 [工具] 功能表中,選取 [NuGet 套件管理員]> [套件管理員主控台]。
在 PMC 中,輸入下列命令:
Add-Migration Rating
Update-Database
Add-Migration
命令會告知架構,以便:
Movie
模型與 Movie
資料庫結構描述。"Rating" 是用來命名移轉檔案的任意名稱。 建議您針對移轉檔案使用有意義的名稱,更加實用。
Update-Database
命令會指示架構將結構描述變更套用至資料庫,並保留現有的資料。
如果您刪除資料庫中的所有記錄,初始設定式會將內容植入資料庫,並包含 Rating
欄位。 您可以使用瀏覽器或 Sql Server 物件總管 (SSOX) 的刪除連結來執行這項操作。
另一個選擇是刪除資料庫並使用移轉重新建立資料庫。 若要在 SSOX 中刪除資料庫:
在 SSOX 中選取資料庫。
以滑鼠右鍵按一下資料庫,然後選取 [刪除]。
核取 [關閉現有的連接]。
選取 [確定]。
在 PMC 中,更新資料庫:
Update-Database
執行應用程式,並驗證您可以使用 Rating
欄位建立/編輯/顯示電影。 若未植入資料庫,請在 SeedData.Initialize
方法中設定中斷點。
文件
Razor Pages 上教學課程系列的第 5 部分。
Razor 頁面上教學課程系列的第 6 部分。
Razor Pages 上教學課程系列的第 4 部分。
訓練
事件
4月22日 下午3時 - 4月23日 下午7時
Focus on Modernization and learn how to unlock the benefits of modern app development
Register now