Skip to content

Update model from database #831

@rowanmiller

Description

@rowanmiller

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions