mikenlanggio
mikenlanggio

Reputation: 1137

How to update a model from table database first

I'm create a context from database with command like this:

Scaffold-DbContext "Server=myConnection;Database=myDatabase;Persist Security Info=False;TrustServerCertificate=False;User ID=myUser;Password=myPassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir MyContext -f

After that, I update structure of single table name MyTable1. So, I try to re-run the command with -Tables option like this:

Scaffold-DbContext "Server=myConnection;Database=myDatabase;Persist Security Info=False;TrustServerCertificate=False;User ID=myUser;Password=myPassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir MyContext -Tables MyTable1 -f

I want to update only MyTable1 and keep stable other table.

But, after run that command, all of table in Context file being removed, and it only remains MyTable1 on OnModelCreating.

Thank you so much!

Upvotes: 0

Views: 439

Answers (1)

ChW
ChW

Reputation: 3348

As far as I know and have read about EF Core, database first approach is only supported to generate a context once. After creating a context from an existing database you have to use migrations for database modifications. Scaffold-DbContext will re-created the whole context on every execution. (Here is the source.)
Of Course you could re-create after every database modificytion a new context - like mentioned in the linked article, but I would suggest to use the code first approach. You could better track all your database changes and it supports you and your colleaugs to work on one project at the same time.

Here is a link with further information about -Table switch. It is used to define which tables are included in context at generation process.

All tables in the database schema are reverse engineered into entity types by default. You can limit which tables are reverse engineered by specifying schemas and tables.

The -Schemas parameter in PMC and the --schema option in the CLI can be used to include every table within a schema.

-Tables (PMC) and --table (CLI) can be used to include specific tables.

Upvotes: 1

Related Questions