I have a dbcontext that was a result of scaffolding, now I need to make changes to the db such as adding relationships and deleting some others.
How can I force EF Core to pick up on these changes, but leave everything else untouched?
I have a dbcontext that was a result of scaffolding, now I need to make changes to the db such as adding relationships and deleting some others.
How can I force EF Core to pick up on these changes, but leave everything else untouched?
See that's a shortcoming with EF Core and model creation is that if you have any customization in the DbContext, you'll lose it all if you Scaffold the DBcontext again based on new changes, and the DBContext being used by the program is overwritten by the new DBcontext.
What do you do what do you do to prevent DBContext customization from being lost in this situation?
I have resorted to Scaffolding the output of the Dbcontext to a staging folder in the VS project and done a copy/past operation of what's new in the new dbcontext over to the current DBContext being used by the program.
What other choice do you have?
If you don't have customization in the DBcontext, then by all means, just overwrite the DbContext.cs used by the existing project.
If your Dbcontext is huge, then you could Scaffold output to the staging folder and do a file comparison between the current and new DBContext(s), note the changes and copy/paste code appropriately. Visual Stuido has a file comparison feature you can use.
https://www.meziantou.net/comparing-files-using-visual-studio-code.htm
Not good!
A lot of extra work. Guess, start with comparing DbContext and work your way down from there. Crazy!
And the alternative is to mess with more model customizations.
Been wondering about the whole code first movement and what the selling point is. I can see it being easier for Microsoft to come up with more Books / Authors tutorials due to how quickly one can write two Entity classes and create the db from that. But if I take that away, I come up with nothing as far as benefits for large applications. It all seems contrived.
Thanks and thanks again for including a link on how to do file comparisons.
You're other choice if you are using TFS Express, which is free to download and use, then you can use the Merge feature, becuase TFS is going to stop you from checking in the new dbcontect.cs that overwrote the one in the project folder and force you to do a automatic merger or you do a manual merger between the dbcontext.cs it has in the repository with the one your about to check-in to the repository.
I am sure Github has some kind of merge feature too.
Hi wavemaster,
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.
The code looks like below:
Scaffold-DbContext "Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force.
More details please refer to the following links:
Entity Framework Core tools reference - Package Manager Console in Visual Studio
Razor Pages with Entity Framework Core in ASP.NET Core - Tutorial 1 of 8
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
6 people are following this question.