-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
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 RevEng: Enable generating IEntityTypeConfiguration classes (avoids large OnModelCreating) #8434 & SqlServer RevEng: Scaffold default schema #9318)
- Create a scaffolding model differ
- Update
IScaffoldingCodeGeneratorto handle new concepts (e.g. inheritance)