How to update models in VS 2019 from database in Entity Framework in ASP.NET Core

10,980

Solution 1

Try to recreate your models via Scaffold-DbContext with "Force" param.

Scaffold-DbContext "Data Source=yoursource;Initial Catalog=yourdb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Force

Solution 2

From the document:

https://docs.microsoft.com/en-us/ef/ef6/modeling/designer/workflows/database-first

The first step is to make some changes to the database schema. We’re going to add a Users table to the schema.

  • Right-click on the database in Server Explorer and select New Query
  • Copy the following SQL into the new query, then right-click on the query and select Execute
CREATE TABLE [dbo].[Users]
(
    [Username] NVARCHAR(50) NOT NULL PRIMARY KEY,  
    [DisplayName] NVARCHAR(MAX) NULL
)

Now that the schema is updated, it’s time to update the model with those changes.

  • Right-click on an empty spot of your model in the EF Designer and select ‘Update Model from Database…’, this will launch the Update Wizard
  • On the Add tab of the Update Wizard check the box next to Tables, this indicates that we want to add any new tables from the schema. The Refresh tab shows any existing tables in the model that will be checked for changes during the update. The Delete tabs show any tables that have been removed from the schema and will also be removed from the model as part of the update. The information on these two tabs is automatically detected and is provided for informational purposes only, you cannot change any settings.

    Refresh Wizard

  • Click Finish on the Update Wizard

     

The model is now updated to include a new User entity that maps to the Users table we added to the database.

Model Updated

Share:
10,980
Mohamed Elkast
Author by

Mohamed Elkast

Updated on June 04, 2022

Comments

  • Mohamed Elkast
    Mohamed Elkast about 2 years

    By VS 2019, I created an API project (database first), everything it's working well, now I made some changes in the database like add new tables and some property and modify data type for others and so on.., the question is how to update models to match the database in SQL?

    this is a screenshot for my project:

    enter image description here

  • Mohamed Elkast
    Mohamed Elkast over 4 years
    thanks a lot for your support, now I updated the data in SQL but the problem in this step in VS Right-click on an empty spot of your model in the EF Designer and select ‘Update Model from Database…’, this will launch the Update Wizard... in my model (in img I attached) How to do that?
  • Mohamed Elkast
    Mohamed Elkast over 4 years
    in vs2019? the object type of ado.net edmx is not available.
  • Nagendra Upwanshi
    Nagendra Upwanshi about 4 years
    Hi Considering given question is for EF Core , your provided answer is fit for EF 6
  • MiguelSlv
    MiguelSlv about 3 years
    if you want to expand your dbContext class, instead of creating another DbContext class, use option -Context. Generated code will be placed on a separated partial class file.
  • Henrik Erlandsson
    Henrik Erlandsson about 2 years
    It seems this information is still provided as current under the heading of EF6, however it seems EF Designer Support was dropped in 6.3.0? But you somehow work around it and open this entire window which is still left in VS 2022 somehow? docs.microsoft.com/en-us/ef/ef6/what-is-new