aspnetcore/tutorials/razor-pages/new-field.md
:::moniker range=">= aspnetcore-10.0"
In this section, Entity Framework Core (EF Core) is used to define the database schema based on the app's model class:
The EF Core approach allows for a more agile development process. You work on the app's data model directly while EF Core creates and synchronizes the database schema. You don't need to switch contexts to and from a database management tool. For an overview of Entity Framework Core and its benefits, see Entity Framework Core.
Using EF Core to automatically create and track a database:
__EFMigrationsHistory table to the database to track whether the schema of the database is in sync with the model classes it was generated from.Automatic verification that the schema and model are in sync makes it easier to find inconsistent database code issues.
Open the Models/Movie.cs file and add a Rating property:
[!code-csharp]
Edit Pages/Movies/Index.cshtml, and add a Rating field:
[!code-cshtml]
Update the following pages with a Rating field:
The app doesn't work until you update the database to include the new field. Running the app without an update to the database throws a SqlException:
SqlException: Invalid column name 'Rating'.
The SqlException exception occurs because the updated Movie model class doesn't match the schema of the Movie table in the database. The database table doesn't have a Rating column.
You can resolve the error by using one of the following approaches:
For this tutorial, use EF Core Migrations.
Update the SeedData class so that it provides a value for the new column. A sample change is shown in the following code, but make this change for each new Movie block.
See the completed SeedData.cs file.
Build the app
Press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd>.
From the View menu, select Terminal and enter the following command:
dotnet build
From the Tools menu, select NuGet Package Manager > Package Manager Console.
In the Package Manager Console (PMC), enter the following command:
Add-Migration Rating
The Add-Migration command tells the framework to:
Movie model with the Movie database schema.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.
In the PMC, enter the following command:
Update-Database
The Update-Database command tells the framework to apply the schema changes to the database and to preserve existing data.
To delete all the records in the database, the initializer seeds the database and includes the Rating field. You can delete records by using 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. To delete the database in SSOX:
Select the database in SSOX.
Right-click on the database, and select Delete.
Check Close existing connections.
Select OK.
In the PMC, update the database:
Update-Database
Use the following commands to add a migration for the rating field:
dotnet ef migrations add rating
dotnet ef database update
The dotnet ef migrations add rating command tells the framework to:
Movie model with the Movie database schema.The name rating is arbitrary and is used to name the migration file. Use a meaningful name for the migration file.
The dotnet ef database update command applies the schema changes to the database and preserves existing data.
If you delete all the records in the database, the initializer seeds the database and includes the Rating field.
Skip this section if you successfully migrated the database.
In this tutorial, use Entity Framework Core migrations features when possible. Migrations update the database schema to match changes in the data model. However, migrations can only make changes that the EF Core provider supports, and some providers have limited capabilities. For example, adding a column might be supported, but removing or changing a column might not be. If you create a migration to remove or change a column, the ef migrations add command succeeds but the ef database update command fails. Due to these limitations, you can drop and re-create the database.
To work around these limitations, manually write migrations code to perform a table rebuild when something in the table changes. A table rebuild involves:
For more information, see the following resources:
Delete the migration folder.
Use the following commands to recreate the database.
dotnet ef database drop
dotnet ef migrations add InitialCreate
dotnet ef database update
Run the app and verify you can create, edit, and display movies with a Rating field. If the database isn't seeded, set a break point in the SeedData.Initialize method.
[!div class="step-by-step"] Previous: Add Search Next: Add Validation
:::moniker-end