第 7 部分,将新字段添加到 ASP.NET Core 中的 Razor 页面Part 7, add a new field to a Razor Page in ASP.NET Core

作者:Rick AndersonBy Rick Anderson

查看或下载示例代码如何下载)。View or download sample code (how to download).

在此部分中,Entity Framework Code First 迁移用于:In this section Entity Framework Code First Migrations is used to:

  • 将新字段添加到模型。Add a new field to the model.
  • 将新字段架构更改迁移到数据库。Migrate the new field schema change to the database.

使用 EF Code First 自动创建数据库时,Code First 将:When using EF Code First to automatically create a database, Code First:

  • 向数据库添加 __EFMigrationsHistory 表格,以跟踪数据库的架构是否与从生成它的模型类同步。Adds an __EFMigrationsHistory table to the database to track whether the schema of the database is in sync with the model classes it was generated from.
  • 如果该模型类未与数据库同步,EF 将引发异常。If the model classes aren't in sync with the database, EF throws an exception.

自动验证架构与模型是否同步可以更容易地发现不一致的数据库代码问题。Automatic verification that the schema and model are in sync makes it easier to find inconsistent database code issues.

向电影模型添加分级属性Adding a Rating Property to the Movie Model

  1. 打开 Models/Movie.cs 文件,并添加 Rating 属性:Open the Models/Movie.cs file and add a Rating property:

    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; }
    }
    
  2. 构建应用程序。Build the app.

  3. 编辑 Pages/Movies/Index.cshtml,并添加 Rating 字段:Edit Pages/Movies/Index.cshtml, and add a Rating field:

    @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>
            Title: <input type="text" asp-for="SearchString" />
            <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>
    
  4. 更新以下页面:Update the following pages:

    1. Rating 字段添加到“删除”和“详细信息”页面。Add the Rating field to the Delete and Details pages.
    2. 使用 Rating 字段更新 Create.cshtmlUpdate Create.cshtml with a Rating field.
    3. Rating 字段添加到“编辑”页面。Add the Rating field to the Edit Page.

在数据库更新为包括新字段之前,应用将不会正常工作。The app won't work until the database is updated to include the new field. 在不更新数据库的情况下运行应用会引发 SqlExceptionRunning the app without an update to the database throws a SqlException:

SqlException: Invalid column name 'Rating'.

SqlException 异常是由于更新的 Movie 模型类与数据库的 Movie 表架构不同导致的。The SqlException exception is caused by the updated Movie model class being different than the schema of the Movie table of the database. 数据库表中没有 Rating 列。There's no Rating column in the database table.

可通过几种方法解决此错误:There are a few approaches to resolving the error:

  1. 让 Entity Framework 自动丢弃并使用新的模型类架构重新创建数据库。Have the Entity Framework automatically drop and re-create the database using the new model class schema. 此方法在开发周期早期很方便,通过该方法可以一起快速改进模型和数据库架构。This approach is convenient early in the development cycle, it allows you to quickly evolve the model and database schema together. 此方法的缺点是会导致数据库中的现有数据丢失。The downside is that you lose existing data in the database. 请勿对生产数据库使用此方法!Don't use this approach on a production database! 在架构更改时丢弃数据库,并使用初始化表达式通过测试数据自动设定数据库种子,这通常是开发应用的有效方式。Dropping the database on schema changes and using an initializer to automatically seed the database with test data is often a productive way to develop an app.

  2. 对现有数据库架构进行显式修改,使它与模型类相匹配。Explicitly modify the schema of the existing database so that it matches the model classes. 此方法的优点是可以保留数据。The advantage of this approach is to keep the data. 可以手动或通过创建数据库更改脚本进行此更改。Make this change either manually or by creating a database change script.

  3. 使用 Code First 迁移更新数据库架构。Use Code First Migrations to update the database schema.

对于本教程,请使用 Code First 迁移。For this tutorial, use Code First Migrations.

更新 SeedData 类,使它提供新列的值。Update the SeedData class so that it provides a value for the new column. 示例更改如下所示,但对每个 new Movie 块做出此更改。A sample change is shown below, but make this change for each new Movie block.

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 文件See the completed SeedData.cs file.

生成解决方案。Build the solution.

添加用于评级字段的迁移Add a migration for the rating field

  1. 从“工具”菜单中,选择“NuGet 包管理器”>“包管理器控制台”。 From the Tools menu, select NuGet Package Manager > Package Manager Console.

  2. 在 PMC 中,输入以下命令:In the PMC, enter the following commands:

    Add-Migration Rating
    Update-Database
    

Add-Migration 命令会通知框架执行以下操作:The Add-Migration command tells the framework to:

  • Movie 模型与 Movie 数据库架构进行比较。Compare the Movie model with the Movie database schema.
  • 创建代码以将数据库架构迁移到新模型。Create code to migrate the database schema to the new model.

名称“Rating”是任意的,用于对迁移文件进行命名。The name "Rating" is arbitrary and is used to name the migration file. 为迁移文件使用有意义的名称是有帮助的。It's helpful to use a meaningful name for the migration file.

Update-Database 命令指示框架将架构更改应用到数据库并保留现有数据。The Update-Database command tells the framework to apply the schema changes to the database and to preserve existing data.

如果删除数据库中的所有记录,初始化表达式会设定数据库种子,并将包括 Rating 字段。If you delete all the records in the database, the initializer will seed the database and include the Rating field. 可以使用浏览器中的删除链接,也可以从 Sql Server 对象资源管理器 (SSOX) 执行此操作。You can do this with the delete links in the browser or from Sql Server Object Explorer (SSOX).

另一个方案是删除数据库,并使用迁移来重新创建该数据库。Another option is to delete the database and use migrations to re-create the database. 删除 SSOX 中的数据库:To delete the database in SSOX:

  1. 在 SSOX 中选择数据库。Select the database in SSOX.

  2. 右键单击数据库,并选择“删除”。Right-click on the database, and select Delete.

  3. 检查“关闭现有连接”。Check Close existing connections.

  4. 选择“确定”。Select OK.

  5. PMC 中更新数据库:In the PMC, update the database:

    Update-Database
    

运行应用,并验证是否可以创建/编辑/显示具有 Rating 字段的电影。Run the app and verify you can create/edit/display movies with a Rating field. 如果数据库未设定种子,则在 SeedData.Initialize 方法中设置断点。If the database isn't seeded, set a break point in the SeedData.Initialize method.

其他资源Additional resources

查看或下载示例代码如何下载)。View or download sample code (how to download).

在此部分中,Entity Framework Code First 迁移用于:In this section Entity Framework Code First Migrations is used to:

  • 将新字段添加到模型。Add a new field to the model.
  • 将新字段架构更改迁移到数据库。Migrate the new field schema change to the database.

使用 EF Code First 自动创建数据库时,Code First 将:When using EF Code First to automatically create a database, Code First:

  • 向数据库添加 __EFMigrationsHistory 表格,以跟踪数据库的架构是否与从生成它的模型类同步。Adds an __EFMigrationsHistory table to the database to track whether the schema of the database is in sync with the model classes it was generated from.
  • 如果该模型类未与数据库同步,EF 将引发异常。If the model classes aren't in sync with the database, EF throws an exception.

自动验证架构与模型是否同步可以更容易地发现不一致的数据库代码问题。Automatic verification that the schema and model are in sync makes it easier to find inconsistent database code issues.

向电影模型添加分级属性Adding a Rating Property to the Movie Model

  1. 打开 Models/Movie.cs 文件,并添加 Rating 属性:Open the Models/Movie.cs file and add a Rating property:

    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; }
    }
    
  2. 构建应用程序。Build the app.

  3. 编辑 Pages/Movies/Index.cshtml,并添加 Rating 字段:Edit Pages/Movies/Index.cshtml, and add a Rating field:

    @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>
            Title: <input type="text" asp-for="SearchString" />
            <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>
    
  4. 更新以下页面:Update the following pages:

    1. Rating 字段添加到“删除”和“详细信息”页面。Add the Rating field to the Delete and Details pages.
    2. 使用 Rating 字段更新 Create.cshtmlUpdate Create.cshtml with a Rating field.
    3. Rating 字段添加到“编辑”页面。Add the Rating field to the Edit Page.

在数据库更新为包括新字段之前,应用将不会正常工作。The app won't work until the database is updated to include the new field. 在不更新数据库的情况下运行应用会引发 SqlExceptionRunning the app without an update to the database throws a SqlException:

SqlException: Invalid column name 'Rating'.

SqlException 异常是由于更新的 Movie 模型类与数据库的 Movie 表架构不同导致的。The SqlException exception is caused by the updated Movie model class being different than the schema of the Movie table of the database. 数据库表中没有 Rating 列。There's no Rating column in the database table.

可通过几种方法解决此错误:There are a few approaches to resolving the error:

  1. 让 Entity Framework 自动丢弃并使用新的模型类架构重新创建数据库。Have the Entity Framework automatically drop and re-create the database using the new model class schema. 此方法在开发周期早期很方便,通过该方法可以一起快速改进模型和数据库架构。This approach is convenient early in the development cycle, it allows you to quickly evolve the model and database schema together. 此方法的缺点是会导致数据库中的现有数据丢失。The downside is that you lose existing data in the database. 请勿对生产数据库使用此方法!Don't use this approach on a production database! 在架构更改时丢弃数据库,并使用初始化表达式通过测试数据自动设定数据库种子,这通常是开发应用的有效方式。Dropping the database on schema changes and using an initializer to automatically seed the database with test data is often a productive way to develop an app.

  2. 对现有数据库架构进行显式修改,使它与模型类相匹配。Explicitly modify the schema of the existing database so that it matches the model classes. 此方法的优点是可以保留数据。The advantage of this approach is to keep the data. 可以手动或通过创建数据库更改脚本进行此更改。Make this change either manually or by creating a database change script.

  3. 使用 Code First 迁移更新数据库架构。Use Code First Migrations to update the database schema.

对于本教程,请使用 Code First 迁移。For this tutorial, use Code First Migrations.

更新 SeedData 类,使它提供新列的值。Update the SeedData class so that it provides a value for the new column. 示例更改如下所示,但对每个 new Movie 块做出此更改。A sample change is shown below, but make this change for each new Movie block.

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 文件See the completed SeedData.cs file.

生成解决方案。Build the solution.

添加用于评级字段的迁移Add a migration for the rating field

  1. 从“工具”菜单中,选择“NuGet 包管理器”>“包管理器控制台”。 From the Tools menu, select NuGet Package Manager > Package Manager Console.

  2. 在 PMC 中,输入以下命令:In the PMC, enter the following commands:

    Add-Migration Rating
    Update-Database
    

Add-Migration 命令会通知框架执行以下操作:The Add-Migration command tells the framework to:

  • Movie 模型与 Movie 数据库架构进行比较。Compare the Movie model with the Movie database schema.
  • 创建代码以将数据库架构迁移到新模型。Create code to migrate the database schema to the new model.

名称“Rating”是任意的,用于对迁移文件进行命名。The name "Rating" is arbitrary and is used to name the migration file. 为迁移文件使用有意义的名称是有帮助的。It's helpful to use a meaningful name for the migration file.

Update-Database 命令指示框架将架构更改应用到数据库并保留现有数据。The Update-Database command tells the framework to apply the schema changes to the database and to preserve existing data.

如果删除数据库中的所有记录,初始化表达式会设定数据库种子,并将包括 Rating 字段。If you delete all the records in the database, the initializer will seed the database and include the Rating field. 可以使用浏览器中的删除链接,也可以从 Sql Server 对象资源管理器 (SSOX) 执行此操作。You can do this with the delete links in the browser or from Sql Server Object Explorer (SSOX).

另一个方案是删除数据库,并使用迁移来重新创建该数据库。Another option is to delete the database and use migrations to re-create the database. 删除 SSOX 中的数据库:To delete the database in SSOX:

  • 在 SSOX 中选择数据库。Select the database in SSOX.

  • 右键单击数据库,并选择“删除”。Right-click on the database, and select Delete.

  • 检查“关闭现有连接”。Check Close existing connections.

  • 选择“确定”。Select OK.

  • PMC 中更新数据库:In the PMC, update the database:

    Update-Database
    

运行应用,并验证是否可以创建/编辑/显示具有 Rating 字段的电影。Run the app and verify you can create/edit/display movies with a Rating field. 如果数据库未设定种子,则在 SeedData.Initialize 方法中设置断点。If the database isn't seeded, set a break point in the SeedData.Initialize method.

其他资源Additional resources

查看或下载示例代码如何下载)。View or download sample code (how to download).

在此部分中,Entity Framework Code First 迁移用于:In this section Entity Framework Code First Migrations is used to:

  • 将新字段添加到模型。Add a new field to the model.
  • 将新字段架构更改迁移到数据库。Migrate the new field schema change to the database.

使用 EF Code First 自动创建数据库时,Code First 将:When using EF Code First to automatically create a database, Code First:

  • 向数据库添加 __EFMigrationsHistory 表格,以跟踪数据库的架构是否与从生成它的模型类同步。Adds an __EFMigrationsHistory table to the database to track whether the schema of the database is in sync with the model classes it was generated from.
  • 如果该模型类未与数据库同步,EF 将引发异常。If the model classes aren't in sync with the database, EF throws an exception.

自动验证架构与模型是否同步可以更容易地发现不一致的数据库代码问题。Automatic verification that the schema and model are in sync makes it easier to find inconsistent database code issues.

向电影模型添加分级属性Adding a Rating Property to the Movie Model

打开 Models/Movie.cs 文件,并添加 Rating 属性:Open the Models/Movie.cs file and add a Rating property:

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; }
}

构建应用程序。Build the app.

编辑 Pages/Movies/Index.cshtml,并添加 Rating 字段:Edit Pages/Movies/Index.cshtml, and add a Rating field:

@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>
        Title: <input type="text" asp-for="SearchString" />
        <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>

更新以下页面:Update the following pages:

  • Rating 字段添加到“删除”和“详细信息”页面。Add the Rating field to the Delete and Details pages.
  • 使用 Rating 字段更新 Create.cshtmlUpdate Create.cshtml with a Rating field.
  • Rating 字段添加到“编辑”页面。Add the Rating field to the Edit Page.

在数据库更新为包括新字段之前,应用将不会正常工作。The app won't work until the database is updated to include the new field. 如果应用立即运行,应用会引发 SqlExceptionIf the app is run now, the app throws a SqlException:

SqlException: Invalid column name 'Rating'.

此错误是由于更新的 Movie 模型类与数据库的 Movie 表架构不同导致的。This error is caused by the updated Movie model class being different than the schema of the Movie table of the database. 数据库表中没有 Rating 列。There's no Rating column in the database table.

可通过几种方法解决此错误:There are a few approaches to resolving the error:

  1. 让 Entity Framework 自动丢弃并使用新的模型类架构重新创建数据库。Have the Entity Framework automatically drop and re-create the database using the new model class schema. 此方法在开发周期早期很方便,通过该方法可以一起快速改进模型和数据库架构。This approach is convenient early in the development cycle, it allows you to quickly evolve the model and database schema together. 此方法的缺点是会导致数据库中的现有数据丢失。The downside is that you lose existing data in the database. 请勿对生产数据库使用此方法!Don't use this approach on a production database! 在架构更改时丢弃数据库,并使用初始化表达式通过测试数据自动设定数据库种子,这通常是开发应用的有效方式。Dropping the database on schema changes and using an initializer to automatically seed the database with test data is often a productive way to develop an app.

  2. 对现有数据库架构进行显式修改,使它与模型类相匹配。Explicitly modify the schema of the existing database so that it matches the model classes. 此方法的优点是可以保留数据。The advantage of this approach is to keep the data. 可以手动或通过创建数据库更改脚本进行此更改。Make this change either manually or by creating a database change script.

  3. 使用 Code First 迁移更新数据库架构。Use Code First Migrations to update the database schema.

对于本教程,请使用 Code First 迁移。For this tutorial, use Code First Migrations.

更新 SeedData 类,使它提供新列的值。Update the SeedData class so that it provides a value for the new column. 示例更改如下所示,但对每个 new Movie 块做出此更改。A sample change is shown below, but make this change for each new Movie block.

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 文件See the completed SeedData.cs file.

生成解决方案。Build the solution.

添加用于评级字段的迁移Add a migration for the rating field

从“工具”菜单中,选择“NuGet 包管理器”>“包管理器控制台”。 From the Tools menu, select NuGet Package Manager > Package Manager Console. 在 PMC 中,输入以下命令:In the PMC, enter the following commands:

Add-Migration Rating
Update-Database

Add-Migration 命令会通知框架执行以下操作:The Add-Migration command tells the framework to:

  • Movie 模型与 Movie 数据库架构进行比较。Compare the Movie model with the Movie database schema.
  • 创建代码以将数据库架构迁移到新模型。Create code to migrate the database schema to the new model.

名称“Rating”是任意的,用于对迁移文件进行命名。The name "Rating" is arbitrary and is used to name the migration file. 为迁移文件使用有意义的名称是有帮助的。It's helpful to use a meaningful name for the migration file.

Update-Database 命令指示框架将架构更改应用到数据库。The Update-Database command tells the framework to apply the schema changes to the database.

如果删除数据库中的所有记录,初始化表达式会设定数据库种子,并将包括 Rating 字段。If you delete all the records in the database, the initializer will seed the database and include the Rating field. 可以使用浏览器中的删除链接,也可以从 Sql Server 对象资源管理器 (SSOX) 执行此操作。You can do this with the delete links in the browser or from Sql Server Object Explorer (SSOX).

另一个方案是删除数据库,并使用迁移来重新创建该数据库。Another option is to delete the database and use migrations to re-create the database. 删除 SSOX 中的数据库:To delete the database in SSOX:

  • 在 SSOX 中选择数据库。Select the database in SSOX.

  • 右键单击数据库,并选择“删除”。Right-click on the database, and select Delete.

  • 检查“关闭现有连接”。Check Close existing connections.

  • 选择“确定”。Select OK.

  • PMC 中更新数据库:In the PMC, update the database:

    Update-Database
    

运行应用,并验证是否可以创建/编辑/显示具有 Rating 字段的电影。Run the app and verify you can create/edit/display movies with a Rating field. 如果数据库未设定种子,则在 SeedData.Initialize 方法中设置断点。If the database isn't seeded, set a break point in the SeedData.Initialize method.

其他资源Additional resources