简体   繁体   中英

Entity Framework Core scaffold additional tables not context

From Microsoft documentation:

https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

When trying to reverse engineer your models and dbcontext from an existing database, you can run a command like this:

Scaffold-DbContext "Server=myserver; Database=mydatabase; Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entities -Tables FirstTable, SecondTable 

This will create me two models and a dbcontext based on my connection details.

If I want to come back later and add another few tables, is it possible to run the same command, or something similar with different parameters that will not create the dbcontext again , as I already have this from my initial scaffold.

Currently we are running the command over and over again and deleting the context each time, or manually building any new models based on the database tables. It seems counter productive.

In Entity Framework 6 and below we would just maintain the edmx diagram but obviously this is not included in EfCore.

In EFCore, there really isn't support for database first design. The scaffolding feature is only meant to be a one-time operation to build your code first models/contexts so that you don't have to hand code them in order to get started using EF. Once run the first time, you should be moving to EF Migrations instead of scaffolding to deal with additions/updates/deletes to the schema.

Here is MS documentation for migrations: https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/

It's logged as a request with the EF Core team, but has been pending for a good while now.

Github link

You can just re-generate the files again, all the generated classes are partial, and you can implement any customizations in parrallel partial classes.

EF Core Power Tools allows you to just generate the POCO classes or just the DbContext.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM