Reputation: 1123
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.
Upvotes: 102
Views: 164005
Reputation: 1
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
Upvotes: 0
Reputation: 11
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
Upvotes: 0
Reputation: 319
you may
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
Upvotes: 0
Reputation: 15193
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.
Upvotes: 3
Reputation: 488
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.
Upvotes: 4
Reputation: 865
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
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.
Upvotes: 2
Reputation: 545
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)
Upvotes: 4
Reputation: 109
You need to do a migration DO NOT rescaffold or you will lose any work done on the models, such as data validations.
Upvotes: 10
Reputation: 545
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.
Upvotes: 4
Reputation: 9200
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!
Upvotes: 63
Reputation: 30120
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.
Upvotes: 150
Reputation: 3
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
Upvotes: -13