从EF迁移到EF核心时,您需要使用整个数据库结构创建新的迁移?
我正在从.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的行为,EF核心迁移应该能够在上称为空的db 时能够创建完整的DB架构,因此添加移民会生成初始迁移(所有现有的EF4.7迁移赢得了'' t可用于您的EFCORE 6程序)。
同时,初始迁移将在现有DB 上失败,因为它已经具有所有表/列/视图。
解决方案如下:添加代码添加代码在迁移的开头中检查其中一张表是否已经存在:
在这里,迁移中的检查表如何存在: 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:
Here how check table existence inside migration: https://entityframeworkcore.com/knowledge-base/53473747/check-if-a-table-exists-using-ef-core-2-1