22 KiB
title | author | description | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|
Part 8, add a new field to an ASP.NET Core MVC app | wadepickett | Part 8 of tutorial series on ASP.NET Core MVC. | wpickett | mvc | 10/25/2022 | tutorials/first-mvc-app/new-field |
Part 8, add a new field to an ASP.NET Core MVC app
:::moniker range=">= aspnetcore-7.0"
In this section Entity Framework Code First Migrations is used to:
- Add a new field to the model.
- Migrate the new field to the database.
When EF Code First is used to automatically create a database, Code First:
- Adds a table to the database to track the schema of the database.
- Verifies the database is in sync with the model classes it was generated from. If they aren't in sync, EF throws an exception. This makes it easier to find inconsistent database/code issues.
Add a Rating Property to the Movie Model
Add a Rating
property to Models/Movie.cs
:
Build the app
Visual Studio
Ctrl+Shift+B
Visual Studio Code
dotnet build
Visual Studio for Mac
Command ⌘ + B
Because you've added a new field to the Movie
class, you need to update the property binding list so this new property will be included. In MoviesController.cs
, update the [Bind]
attribute for both the Create
and Edit
action methods to include the Rating
property:
[Bind("Id,Title,ReleaseDate,Genre,Price,Rating")]
Update the view templates in order to display, create, and edit the new Rating
property in the browser view.
Edit the /Views/Movies/Index.cshtml
file and add a Rating
field:
Update the /Views/Movies/Create.cshtml
with a Rating
field.
Visual Studio / Visual Studio for Mac
You can copy/paste the previous "form group" and let intelliSense help you update the fields. IntelliSense works with Tag Helpers.
Visual Studio Code
Update the remaining templates.
Update the SeedData
class so that it provides a value for the new column. A sample change is shown below, but you'll want to make this change for each new Movie
.
The app won't work until the DB is updated to include the new field. If it's run now, the following SqlException
is thrown:
SqlException: Invalid column name 'Rating'.
This error occurs because the updated Movie model class is different than the schema of the Movie table of the existing database. (There's no Rating
column in the database table.)
There are a few approaches to resolving the error:
-
Have the Entity Framework automatically drop and re-create the database based on the new model class schema. This approach is very convenient early in the development cycle when you're doing active development on a test database; it allows you to quickly evolve the model and database schema together. The downside, though, is that you lose existing data in the database — so you don't want to use this approach on a production database! Using an initializer to automatically seed a database with test data is often a productive way to develop an application. This is a good approach for early development and when using SQLite.
-
Explicitly modify the schema of the existing database so that it matches the model classes. The advantage of this approach is that you keep your data. You can make this change either manually or by creating a database change script.
-
Use Code First Migrations to update the database schema.
For this tutorial, Code First Migrations is used.
Visual Studio
From the Tools menu, select NuGet Package Manager > Package Manager Console.
In the PMC, enter the following commands:
Add-Migration Rating
Update-Database
The Add-Migration
command tells the migration framework to examine the current Movie
model with the current Movie
DB schema and create the necessary code to migrate the DB to the new model.
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.
If all the records in the DB are deleted, the initialize method will seed the DB and include the Rating
field.
Visual Studio Code / Visual Studio for Mac
Delete the Migrations folder and the database file, and then run the following .NET CLI commands:
dotnet ef migrations add InitialCreate
dotnet ef database update
For more information, see Resetting all migrations.
Run the app and verify you can create, edit, and display movies with a Rating
field.
:::moniker-end
:::moniker range="= aspnetcore-6.0"
In this section Entity Framework Code First Migrations is used to:
- Add a new field to the model.
- Migrate the new field to the database.
When EF Code First is used to automatically create a database, Code First:
- Adds a table to the database to track the schema of the database.
- Verifies the database is in sync with the model classes it was generated from. If they aren't in sync, EF throws an exception. This makes it easier to find inconsistent database/code issues.
Add a Rating Property to the Movie Model
Add a Rating
property to Models/Movie.cs
:
Build the app
Visual Studio
Ctrl+Shift+B
Visual Studio Code
dotnet build
Visual Studio for Mac
Command ⌘ + B
Because you've added a new field to the Movie
class, you need to update the property binding list so this new property will be included. In MoviesController.cs
, update the [Bind]
attribute for both the Create
and Edit
action methods to include the Rating
property:
[Bind("Id,Title,ReleaseDate,Genre,Price,Rating")]
Update the view templates in order to display, create, and edit the new Rating
property in the browser view.
Edit the /Views/Movies/Index.cshtml
file and add a Rating
field:
Update the /Views/Movies/Create.cshtml
with a Rating
field.
Visual Studio / Visual Studio for Mac
You can copy/paste the previous "form group" and let intelliSense help you update the fields. IntelliSense works with Tag Helpers.
Visual Studio Code
Update the remaining templates.
Update the SeedData
class so that it provides a value for the new column. A sample change is shown below, but you'll want to make this change for each new Movie
.
The app won't work until the DB is updated to include the new field. If it's run now, the following SqlException
is thrown:
SqlException: Invalid column name 'Rating'.
This error occurs because the updated Movie model class is different than the schema of the Movie table of the existing database. (There's no Rating
column in the database table.)
There are a few approaches to resolving the error:
-
Have the Entity Framework automatically drop and re-create the database based on the new model class schema. This approach is very convenient early in the development cycle when you're doing active development on a test database; it allows you to quickly evolve the model and database schema together. The downside, though, is that you lose existing data in the database — so you don't want to use this approach on a production database! Using an initializer to automatically seed a database with test data is often a productive way to develop an application. This is a good approach for early development and when using SQLite.
-
Explicitly modify the schema of the existing database so that it matches the model classes. The advantage of this approach is that you keep your data. You can make this change either manually or by creating a database change script.
-
Use Code First Migrations to update the database schema.
For this tutorial, Code First Migrations is used.
Visual Studio
From the Tools menu, select NuGet Package Manager > Package Manager Console.
In the PMC, enter the following commands:
Add-Migration Rating
Update-Database
The Add-Migration
command tells the migration framework to examine the current Movie
model with the current Movie
DB schema and create the necessary code to migrate the DB to the new model.
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.
If all the records in the DB are deleted, the initialize method will seed the DB and include the Rating
field.
Visual Studio Code / Visual Studio for Mac
See Resetting all migrations to remove all migrations and start over.
Run the app and verify you can create, edit, and display movies with a Rating
field.
:::moniker-end
:::moniker range="= aspnetcore-5.0"
In this section Entity Framework Code First Migrations is used to:
- Add a new field to the model.
- Migrate the new field to the database.
When EF Code First is used to automatically create a database, Code First:
- Adds a table to the database to track the schema of the database.
- Verifies the database is in sync with the model classes it was generated from. If they aren't in sync, EF throws an exception. This makes it easier to find inconsistent database/code issues.
Add a Rating Property to the Movie Model
Add a Rating
property to Models/Movie.cs
:
Build the app
Visual Studio
Ctrl+Shift+B
Visual Studio Code
dotnet build
Visual Studio for Mac
Command ⌘ + B
Because you've added a new field to the Movie
class, you need to update the property binding list so this new property will be included. In MoviesController.cs
, update the [Bind]
attribute for both the Create
and Edit
action methods to include the Rating
property:
[Bind("Id,Title,ReleaseDate,Genre,Price,Rating")]
Update the view templates in order to display, create, and edit the new Rating
property in the browser view.
Edit the /Views/Movies/Index.cshtml
file and add a Rating
field:
Update the /Views/Movies/Create.cshtml
with a Rating
field.
Visual Studio / Visual Studio for Mac
You can copy/paste the previous "form group" and let intelliSense help you update the fields. IntelliSense works with Tag Helpers.
Visual Studio Code
Update the remaining templates.
Update the SeedData
class so that it provides a value for the new column. A sample change is shown below, but you'll want to make this change for each new Movie
.
The app won't work until the DB is updated to include the new field. If it's run now, the following SqlException
is thrown:
SqlException: Invalid column name 'Rating'.
This error occurs because the updated Movie model class is different than the schema of the Movie table of the existing database. (There's no Rating
column in the database table.)
There are a few approaches to resolving the error:
-
Have the Entity Framework automatically drop and re-create the database based on the new model class schema. This approach is very convenient early in the development cycle when you're doing active development on a test database; it allows you to quickly evolve the model and database schema together. The downside, though, is that you lose existing data in the database — so you don't want to use this approach on a production database! Using an initializer to automatically seed a database with test data is often a productive way to develop an application. This is a good approach for early development and when using SQLite.
-
Explicitly modify the schema of the existing database so that it matches the model classes. The advantage of this approach is that you keep your data. You can make this change either manually or by creating a database change script.
-
Use Code First Migrations to update the database schema.
For this tutorial, Code First Migrations is used.
Visual Studio
From the Tools menu, select NuGet Package Manager > Package Manager Console.
In the PMC, enter the following commands:
Add-Migration Rating
Update-Database
The Add-Migration
command tells the migration framework to examine the current Movie
model with the current Movie
DB schema and create the necessary code to migrate the DB to the new model.
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.
If all the records in the DB are deleted, the initialize method will seed the DB and include the Rating
field.
Visual Studio Code / Visual Studio for Mac
See Resetting all migrations to remove all migrations and start over.
Run the app and verify you can create, edit, and display movies with a Rating
field.
:::moniker-end
:::moniker range="< aspnetcore-5.0"
In this section Entity Framework Code First Migrations is used to:
- Add a new field to the model.
- Migrate the new field to the database.
When EF Code First is used to automatically create a database, Code First:
- Adds a table to the database to track the schema of the database.
- Verifies the database is in sync with the model classes it was generated from. If they aren't in sync, EF throws an exception. This makes it easier to find inconsistent database/code issues.
Add a Rating Property to the Movie Model
Add a Rating
property to Models/Movie.cs
:
Build the app
Visual Studio
Ctrl+Shift+B
Visual Studio Code
dotnet build
Visual Studio for Mac
Command ⌘ + B
Because you've added a new field to the Movie
class, you need to update the property binding list so this new property will be included. In MoviesController.cs
, update the [Bind]
attribute for both the Create
and Edit
action methods to include the Rating
property:
[Bind("Id,Title,ReleaseDate,Genre,Price,Rating")]
Update the view templates in order to display, create, and edit the new Rating
property in the browser view.
Edit the /Views/Movies/Index.cshtml
file and add a Rating
field:
Update the /Views/Movies/Create.cshtml
with a Rating
field.
Visual Studio / Visual Studio for Mac
You can copy/paste the previous "form group" and let intelliSense help you update the fields. IntelliSense works with Tag Helpers.
Visual Studio Code
Update the remaining templates.
Update the SeedData
class so that it provides a value for the new column. A sample change is shown below, but you'll want to make this change for each new Movie
.
The app won't work until the DB is updated to include the new field. If it's run now, the following SqlException
is thrown:
SqlException: Invalid column name 'Rating'.
This error occurs because the updated Movie model class is different than the schema of the Movie table of the existing database. (There's no Rating
column in the database table.)
There are a few approaches to resolving the error:
-
Have the Entity Framework automatically drop and re-create the database based on the new model class schema. This approach is very convenient early in the development cycle when you're doing active development on a test database; it allows you to quickly evolve the model and database schema together. The downside, though, is that you lose existing data in the database — so you don't want to use this approach on a production database! Using an initializer to automatically seed a database with test data is often a productive way to develop an application. This is a good approach for early development and when using SQLite.
-
Explicitly modify the schema of the existing database so that it matches the model classes. The advantage of this approach is that you keep your data. You can make this change either manually or by creating a database change script.
-
Use Code First Migrations to update the database schema.
For this tutorial, Code First Migrations is used.
Visual Studio
From the Tools menu, select NuGet Package Manager > Package Manager Console.
In the PMC, enter the following commands:
Add-Migration Rating
Update-Database
The Add-Migration
command tells the migration framework to examine the current Movie
model with the current Movie
DB schema and create the necessary code to migrate the DB to the new model.
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.
If all the records in the DB are deleted, the initialize method will seed the DB and include the Rating
field.
Visual Studio Code / Visual Studio for Mac
See Resetting all migrations to remove all migrations and start over.
Run the app and verify you can create, edit, and display movies with a Rating
field.
:::moniker-end