从EF迁移到EF核心时,您需要使用整个数据库结构创建新的迁移?

发布于 2025-02-02 12:15:18 字数 222 浏览 3 评论 0原文

我正在从.NET Framework 4.7到.NET 6(包括EF到EF Core)迁移API,当我创建dbContext并执行添加> add-Migration命令时,它创建了数据库的整个结构,我害怕运行,因为我不确定会发生什么。

这种行为正常吗?是否有任何方法可以避免此“初始迁移”或事先检查该迁移在数据库上引起任何问题吗?

谢谢。

I'm migrating an API from .NET Framework 4.7 to .NET 6 (including EF to EF Core) and when I create the dbContext and execute the Add-Migration command, it creates the whole structure of the database, which I'm scared of running because I'm not sure what could change.

Is this behaviour normal? Is there any way to avoid this "initial migration" or check beforehand if this migration causes any issue on the database?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

决绝 2025-02-09 12:15:19

这是正确的行为,EF核心迁移应该能够在上称为空的db 时能够创建完整的DB架构,因此添加移民会生成初始迁移(所有现有的EF4.7迁移赢得了'' t可用于您的EFCORE 6程序)。

同时,初始迁移将在现有DB 上失败,因为它已经具有所有表/列/视图。

解决方案如下:添加代码添加代码在迁移的开头中检查其中一张表是否已经存在:

  • 如果是的,则比db结构已经匹配初始迁移,这里无事可做(仅存在),
  • 如果不是,则是空白的DB和最初的EFCORE迁移应进行。

在这里,迁移中的检查表如何存在: https://entityframeworkcore.com/knowledge-base/53473747/check-if-a-table-exists-usiss-using-ef-core-core-2-1

That's correct behaviour, EF Core migrations should be able to create full DB schema when called on an empty DB, so Add-Migration generate initial migration which will do exactly so (all existing EF4.7 migration won't be available for your EFCore 6 program).

At the same time, that initial migration will fail on existing DB because it already has all tables/columns/views.

Solution here is following: add code to the beginning of migration which checks whether one of the tables already exists:

  • If yes, than DB structure is already match initial migration, nothing to do here (just exist)
  • If not, this is blank DB and initial EFCore migration should be ran.

Here how check table existence inside migration: https://entityframeworkcore.com/knowledge-base/53473747/check-if-a-table-exists-using-ef-core-2-1

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