I have created DbContext(Scaffold-DbContext) using EFCore in Web API Core 3.1. and after some changes and created new tables in SQL Server. I want to update new created tables in EFCore Web API Core 3.1. How can I update my all created tables?
I have tried many times as following command. but either all tables repeat or some time all tables name changes like TblUser after change (TblUsers). why?
caffold-DbContext "Server=vaio;Database=Company;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
i have tried as follow
caffold-DbContext "Server=vaio;Database=Company;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Thanks in advance
CodePudding user response:
I had the same issue before. So the EF is all based on code first. There no way to update you models by scaffolding. But there is other way I'm using to solve this issue. I open second project and run scaffold command there like this:
Scaffold-DbContext "conection string here" Microsoft.EntityFrameworkCore.SqlServer -OutputDir "Your Dir" -Force -Tables tabeOne, tableTwo
In that way you get all changes you want in the new project then copy and replace all in your main project from the new one.
CodePudding user response:
efcore is not designed for db first, reverse engineering the model from the database you cannot just run scaffold command again, you should manually update models or delete the models and rerun the scaffold,
Basically you have to add --Force flag to overwrite model with new one
from:
https://docs.microsoft.com/en-us/ef/core/managing-schemas/scaffolding?tabs=dotnet-core-cli
After making changes to the database, you may need to update your EF Core model to reflect those changes. If the database changes are simple, it may be easiest just to manually make the changes to your EF Core model. For example, renaming a table or column, removing a column, or updating a column's type are trivial changes to make in code.
More significant changes, however, are not as easy to make manually. One common workflow is to reverse engineer the model from the database again using -Force (PMC) or --force (CLI) to overwrite the existing model with an updated one.