Entity Framework code-first : how to add more props on model and update database?

asp.net c# entity-framework-core model

Question

I have created a new ASP.NET Core 2.0 project with Visual Studio 2017. Currently I have a Coach model class:

public class Coach
{
        public int ID { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public string Methodology { get; set; }
        public string Price { get; set; }
        public int Rating { get; set; }

        //New property
        public string Email { get; set; }
}

I want to add a new property Email to the Coach class. I also want to create a Review class that is used to review a Coach. My model class User reviews coaches. However when I add new properties and new classes I get this message on startup of web app:

A database operation failed while processing the request.

SqlException: Invalid column name 'Email'.

Applying existing migrations for ApplicationDbContext may resolve this issue. There are migrations for ApplicationDbContext that have not been applied to the database

00000000000000_CreateIdentitySchema
Apply Migrations

In Visual Studio, you can use the Package Manager Console to apply pending migrations to the database:

PM> Update-Database
Alternatively, you can apply pending migrations from a command prompt at your project directory:

dotnet ef database update

Following the message I get does not work. The issue still remains. Can anyone point me in the right direction how to update the code and then update the database that is stored locally? How is the workflow supposed to be like when changing models code-first and wanting database to mirror that change?

1
2
12/3/2017 10:30:45 AM

Accepted Answer

Generating & Running Migrations

Code First Migrations has two primary commands that you need to run.

Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created

Update-Database will apply any pending migrations to the database We need to scaffold a migration to take care of the new Email property we have added. The Add-Migration command allows us to give these migrations a name, let’s just call ours AddEmail.

Run the Add-Migration AddEmail command in Package Manager Console

Run the Update-Database command in Package Manager Console

3
12/3/2017 11:08:59 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow