Noor Uddin
Noor Uddin

Reputation: 1

How to update DB context class in .Net Core using database first approach?

I have an application in ASP web forms. I working on migrating this web form application to .net core 3.0. I am using database first approach as I have perfect tables structure, many store procedures and too much data. Today I add some new columns in data table 'Stock'. Columns name are:

  1. Color_ID. 2. Location_Id

I also add one new table 'Locations' in my database. Now the question is how can I update the DB context model class in my project for this two tables changes using database first approach in .net core.

Upvotes: 0

Views: 2032

Answers (1)

Saeed Gholamzadeh
Saeed Gholamzadeh

Reputation: 842

Scaffold Context

Using the Package Manager Console

Scaffold-DbContext "your_connection_string" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force

using CLI

dotnet ef dbcontext scaffold "your_connection_string" Microsoft.EntityFrameworkCore.SqlServer -o Models -f

Updating the Context

if you want to re-scaffold the model after making schema changes, you can do so by specifying the -force option e.g.:

dotnet ef dbcontext scaffold "your_connection_string" Microsoft.EntityFrameworkCore.SqlServer -force

All of the class files will be overwritten, which means that any amendments that you might have made to them e.g. adding attributes or additional members, will be lost.

Upvotes: 5

Related Questions