SqlException: Invalid column name "..." | ASP.NET Core

This error is caused by the updated Model class. For example, you have the following class named as "Movie".


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; }
    public decimal Price { get; set; }
}

You want to add the "Rating" to the Model class. After adding this property, run your app. Then it will give you "Invalid column name 'Rating'" message.
Because the schema of the "Movie" table of the database doesn’t match with the updated "Movie" Model class.


public class Movie
{
	.
	.
	.
    public string Rating { get; set; }
}

To solve this error, you can use the "Entity Framework Code First Migrations" approach.


Step 1 : In the PMC ( Package Manager Console ), enter the following commands.


	Add-Migration Rating

Add-Migration Rating -> The name 'Rating' is used to name the migration file as a meaningful name.


Step 2 : In the PMC, update the database.


	Update-Database

If you use an initializer, delete test data from Sql Server Object Explorer (SSOX)


The initializer is used to automatically seed the database with test data.


Use of 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 with the "Update-Database" command.


Reference : https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/new-field?view=aspnetcore-2.1