从SQL Server中使用Web API Core 3.1中的EF Core更新新表

发布于 2025-01-17 21:26:38 字数 548 浏览 0 评论 0原文

我已经使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

话少心凉 2025-01-24 21:26:38

我之前也遇到过同样的问题。所以EF都是基于代码优先的。没有办法通过脚手架来更新模型。
但我还用其他方法来解决这个问题。我打开第二个项目并在那里运行脚手架命令,如下所示:

Scaffold-DbContext "conection string here" Microsoft.EntityFrameworkCore.SqlServer -OutputDir "Your Dir" -Force -Tables tabeOne, tableTwo

通过这种方式,您可以在新项目中获得所需的所有更改,然后从新项目中复制并替换主项目中的所有更改。

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.

写下不归期 2025-01-24 21:26:38

EFCORE不是为DB设计的,而是从数据库中进行逆向工程,您不能只再次运行脚手架命令,您应该手动更新模型或删除模型并重新运行脚手架,

基本上您必须将force标志添加到覆盖模型,并使用来自

https: //learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding?tabs=dotnet-core-cli

更改数据库后,您可能需要更新EF
核心模型以反映这些变化。如果数据库更改为
简单,最简单的方法是手动对您的EF进行更改
核心模型。例如,重命名表或列,删除
列或更新列的类型是琐碎的更改
代码。

更重要的更改并不那么容易手动进行。
一个常见的工作流程是将模型从数据库进行逆向工程
再次使用-force(pmc)或 - force(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

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.

迷爱 2025-01-24 21:26:38

最后,我找到了一个解决方案,以更新数据库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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文