102

I used EntityFramework Core database first to create model as illustrated in the EF Core documentation

But I don't know how to update the model when the database has been edit.

2
  • It's called Entity Framework Core 1.0 Commented Aug 3, 2016 at 10:38
  • That's not EF-core documentation and as far as it's about EF, it's not database-first. So in fact this is a one-sentence question "I don't know how to update the model when the database has been edited". I'm afraid that's too broad for a Stack Overflow question. Commented Sep 27, 2023 at 12:11

12 Answers 12

150

You can re-scaffold the model by running the command that you originally ran with the -Force option added. That will result in the contents of the specified folder being over-written. Using the Package Manager Console example from the EF Core docs, the revised command becomes:

Scaffold-DbContext "Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force

Alternatively, if you are using CLI commands, it becomes:

dotnet ef dbcontext scaffold "Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -f

However, you should consider using Migrations to keep your model and database schema in sync with each other. That way you make changes to the model and then propagate them to the database.

Sign up to request clarification or add additional context in comments.

13 Comments

this doesn't work for me shows the error "scaffold-DbContext" not found like that.Help plz
If you receive an error stating The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, then close and reopen Visual Studio.
To know what you need to put in the "" for your connection string, This video really help --> VIDEO
You can use the Package Manager Console -Tables argument (learnentityframeworkcore.com/migrations/commands/…) to specify the table that you want to scaffold: -Tables Products, or if you are using the CLI, the argument is -table (singular) learnentityframeworkcore.com/migrations/commands/…
@SagarPatil You can't. That's not a supported scenario. You will need to recompile the application for any changes to the model to take effect.
|
63

Additional Tip

If you are going to update the models from time to time, here's a convenient way to simplify the process.

Head over to menu Tools > External Tools, and then Add a new menu and fill in the following entries:


Title:

Update DbContext

Command:

dotnet.exe

Arguments:

ef dbcontext scaffold "your-connection-string" Microsoft.EntityFrameworkCore.SqlServer --output-dir=Models --force

Initial directory:

$(ProjectDir)

Then optionally tick "Use Output window", hit Apply and OK.

When you go to Tools again, this new menu should be there and ready for reuse, in just a click of a button!

Comments

10

You need to do a migration DO NOT rescaffold or you will lose any work done on the models, such as data validations.

4 Comments

yes correct, for me I had to do DTO's just to avoid the validations in my models
That's incomplete. If you just perform a migration you'll overwrite database changes that were done by say your DBA and not by code first & you'll need to get your resume ready. If you have to utilize migrations you're going to have to comment out all the changes migration will run against the database and make sure you have properly manually updated all POCO classes and your entity to match the DB. Its probably simplest just to do as Yom S. says. Also, in the future refrain from adding code to your entity POCO classes do as described in the "O" of SOLID. Open for inheritance Closed for update
I think we should never write into a generated code at all. Something is wrong if we do.
the question is about Database first approach, then migration is not applicable here. Migration is for model first approach.
4

If we have customize in dbcontext class E.g. add LoggerFactory and then after we are using ('Scaffold-DbContext "Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force'). this command then all customize changes will be lost.

1 Comment

the DbContext class created is a partial class. You can create a separate file and make your enhancements to the class from there.
4

Use command Add-migration NameOfMigrationfile which create in migration folder at the application level.

If auto migration is not enabled then we can use some below commands in Package Manager Console.

PM > Enable-migrations -force (If automigration not enable) PM > Add-migration MigrationName PM > Update-database -force (If add-migration command not working then we can use udate command)

Comments

4

You can use this extension: EF Core Power Tools , it will make your life much easier, and you will not have to write any command line.

3 Comments

Extension not working with latest EF core sadly... But Seems really nice.
@Iannick I use it daily with the latest versions of VS2022 and EF6 till today, and it works perfectly; if you have any problem, you may need to check here github.com/ErikEJ/EFCorePowerTools/wiki
For anyone still having this issue or getting a "table already exists" error when using the PS commands. This tools built in migration fixed my issues. Saved me so much time. Thanks!
3

For the ones that prefer to keep all in the same DbContext class, use Scaffold-DbContext with option -Context.

example:

Scaffold-DbContext "Server=server;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f -Context MyDbContext

The generated code for the MyDbContext will be placed on a new partial class file, so no code will be lost.

Comments

2

Solution given by Mike worked for me with no problem. A bit of elaborated answer is here based on the answer of Mike.

Scaffold-DbContext "Server={{Server name}};Database={{Database name}};User ID={{Login}};password={{Password}}" Microsoft.EntityFrameworkCore.SqlServer -OutputDir {{Folder name}} -Force
  • Folder name : Folder name under the project. The folder must have to created before executing this command.

Sample

Scaffold-DbContext "Server=.;Database=EmployeeDB;User ID=sa;password=12345" Microsoft.EntityFrameworkCore.SqlServer -OutputDir "./EmployeeDBModel" -Force

In case you are not using SQL Server authentication please refer to the answer given by Mike.

Comments

0

you may

  1. rename folder with scaffolded clasess let's say model->model1
  2. scaffold database with original command
  3. delete model1 folder

but if you have any changes in /model folder you will lose them. i am keeping /Model folder nearly intact and have all changes in /Partial folder (partial classes).

the only thing i need to change manually after rescaffolding isto remove constructor and onconfiguring (etc) from newly created dbcontext class, because i am keeping this code in partial class

Comments

0

If you are using Mysql as your Database then please use this command:

Scaffold-DbContext "localhost;database=mydb;user=myuser;password=mypassword" Pomelo.EntityFrameworkCore.MySql -OutputDir Models -Force

Comments

0

If overwriting changes you made in the generated code is an issue, you can put these changes into their own partial class files. These will not be overwritten when re-scaffolding from a changed database. As is also recommended here:

https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=vs#repeated-scaffolding

Comments

-13

Open your ContextModel.edmx file to display the model diagram. Right-click anywhere on the design surface, and select Update Model from Database... In the Update Wizard, select the Refresh tab and select your table then click Finish button.

For more details with picture visit: EF Database First with ASP.NET MVC: Changing the Database

2 Comments

This is only possible up until EF6 and lower - and not the core variant, as requested.
The question is for EF Core not EF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.