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

后端 未结 3 1919
予麋鹿
予麋鹿 2021-01-07 04:45

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 modif

相关标签:
3条回答
  • 2021-01-07 04:46
    Hi I am Considering you are using EFCore, please confirm !! 
    As per available information over internet 
    EF core for DB first ( from db to EF ) approach to Modify new/existing entity 
    

    you need to follow steps as:

    1. EF model require to run Scaffold with flag Force

    EF model require to run Scaffold with flag Force this will remove all your local changes in model (for me it was IdentityDBContext and override identityUser changes)


    1. after run the all your model migration

    2. running all migration will bring everything in sync bw EF and DB


    now you need to do the local changaes again over solution ( for me update IdentityDBContext and override identityUser changes back as it was before db changes )


    1. also as per below answer please notice EDMX object is not available yet, anyone has any clue then please share here.
    0 讨论(0)
  • 2021-01-07 04:52

    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

    0 讨论(0)
  • 2021-01-07 04:55

    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
    
    0 讨论(0)
提交回复
热议问题