从SQL Server中使用Web API Core 3.1中的EF Core更新新表
我已经使用Web API Core 3.1中的EFCORE创建了DBContext(脚手架DBContext)。经过一些更改并在SQL Server中创建了新表。我想在EFCORE Web API Core 3.1中更新新创建的表。如何更新所有创建的表?
我尝试过多次以下命令。但是,要么所有表重复,要么所有表名称更改(例如TBLUSER)(TBLUSERS)等所有表名称。为什么?
caffold-DbContext "Server=vaio;Database=Company;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
我尝试过 如下所示,
caffold-DbContext "Server=vaio;Database=Company;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
请提前
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我之前也遇到过同样的问题。所以EF都是基于代码优先的。没有办法通过脚手架来更新模型。
但我还用其他方法来解决这个问题。我打开第二个项目并在那里运行脚手架命令,如下所示:
通过这种方式,您可以在新项目中获得所需的所有更改,然后从新项目中复制并替换主项目中的所有更改。
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:
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.
EFCORE不是为DB设计的,而是从数据库中进行逆向工程,您不能只再次运行脚手架命令,您应该手动更新模型或删除模型并重新运行脚手架,
基本上您必须将force标志添加到覆盖模型,并使用来自
:
https: //learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding?tabs=dotnet-core-cli
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://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding?tabs=dotnet-core-cli
最后,我找到了一个解决方案,以更新数据库First DBContext,该解决方案已删除了旧模型文件夹,然后再次运行脚手架dbContext命令。它成功地从SQL Server获取了所有表。
finally i found a solution to update Database first DbContext that i have deleted old Model folder and then again run Scaffold-Dbcontext command. it fetched all tables from Sql server successfully.