question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update model from database

See original GitHub issue

We’ll add a new comand (e.g. Update-DbContext and dotnet ef dbcontext update) to re-scaffold your DbContext and entity types to incorporate any changes you’ve been made to the database schema. This will allow you to update your model to be compatible with the database but preserve any customizations you’ve made (e.g. navigation property names) to the originally scaffolded code.

Scope

The folowing types of customizations will be preserved.

  • Entity type names
  • Entity type property names (including navigation properties)
  • Entity type inheritance
  • DbSet property names

The following are not a priority during the initial implementation.

  • Non-EF annotations on entity type properties (could be enabled via [MetadataType])
  • Configuration that deviates from the database (database always wins)
    • Additional constraints added to the model

Files will be blindly overwritten during this process. To allow additional customizations to the model, we’ll continue generating partial classes and reccommend adding custom members to a new file with another partial definition of the class.

Implementation

We’ll leverage the existing Reverse Engineering assets for this work. Specifically:

  • IDatabaseModelFactory–Reads the database schema.
  • IScaffoldingModelFactory–Transforms the schema into a compatible IModel.
  • IScaffoldingCodeGenerator–Transforms the IModel into code.

We’ll read the current database schema, transform it into a model and compare it to the current model. Anything that has changed will be merged with the current model and the resulting model will be scaffolded into code. Existing files will be overwritten and removed types will be deleted.

During the inital reverse engineer, we scaffold an OnConfiguring() stub. We’ll need to move this into it’s own file so we can generate it once, but not during update.

TODO

  • Refactor generated code to be update friendly (consider #8434 & #9318)
  • Create a scaffolding model differ
  • Update IScaffoldingCodeGenerator to handle new concepts (e.g. inheritance)

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:158
  • Comments:53 (25 by maintainers)

github_iconTop GitHub Comments

28reactions
miloshavlicekcommented, Apr 20, 2020

More than 5 years on backlog. So sad.

11reactions
GunboatDiplomatcommented, Aug 14, 2019

Any updates on this feature? Is it marked for any near future release?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you refresh the model when the db changes?
Right click anywhere on that window and select "Update Model from Database". An Update Wizard window appears. Click Finish to update your model....
Read more >
Create And Update An .edmx File Using Entity Framework ...
In the Model Browser, right-click the .edmx file and select Update Model from Database. Update Model ; Click the Add tab. add ;...
Read more >
Updating Model from Database
1. Right-click the empty space in the diagram area and select Update Model From Database from the popup menu. · 2. Select the...
Read more >
Tutorial: Change the database for EF Database First ...
Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select ......
Read more >
How To Update Entity In Entity Framework In Visual Studio
For updating right click on the edmx icon on Model Browser and select Update Model From Database. How To Update Entity In Entity...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found