EF Core Migrations手动编辑可以吗? [英] EF Core Migrations manual edits possible?

查看:109
本文介绍了EF Core Migrations手动编辑可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在示例项目中使用EF Core 2.0,并且配置了一些有价值的对象。我修改代码并通过CLI命令行生成迁移。在上一次迁移中,它没有像应有的那样添加新的数据库表,而是尝试将现有表彼此重命名并为现有表创建一个额外的表。我不知道原因。

I am using EF Core 2.0 in my sample project with some value object configurations. I modify the code and generate migrations via CLI command line. In the last migration rather than adding a new database table as it should, it is trying to rename existing tables to each other and create an extra table for existing one. I could not figure out the reason for it.

问题是,由于使用EF Core,快照是与迁移本身独立的自动生成的文件,因此我不想修改快照。

Issue is, since with EF Core the snapshot is a separate auto-generated file from the migration itself I don't want to modify the snapshot.

我只想修改迁移脚本,以便它不会重命名多个表,然后从我创建的迁移中生成快照。

I only want to modify the migration script so that it will not rename multiple tables, and then generate the snapshot from the migrations I created.

我在CLI中没有看到与此相关的任何命令-修改支架式迁移并重新生成是一种不好的做法,还是我错过了一些显而易见的新链接,其中解释了如何手动修改迁移脚本?

I did not see any command for this in the CLI - is it such a bad practice to modify the scaffolded migration and regenerate or am I missing some obvious new link where how to manually modify migration scripts is explained?

感谢一堆。

更新1:发表评论后,添加了有关此链接

Update 1: After comments, added info about the snapshot from this link.

由于当前数据库模式以代码表示,因此EF Core无需与数据库进行交互即可创建迁移。添加迁移时,EF通过将数据模型与快照文件进行比较来确定更改的内容。 EF仅在必须更新数据库时才与数据库交互。 +

Because the current database schema is represented in code, EF Core doesn't have to interact with the database to create migrations. When you add a migration, EF determines what changed by comparing the data model to the snapshot file. EF interacts with the database only when it has to update the database. +

推荐答案

我检查了从源代码管理生成的快照代码。它正是根据我的需要添加了一张额外的表。

I examined my generated snapshot code from source control. It exactly has added one extra table as what I needed.

生成此脚本的迁移脚本充其量很忙-将多个表相互重命名,然后警告说这可能会中断,从而导致多个问题。

The migration script to generate this is hectic at best - renaming multiple tables to each other and then warning that this could break causing multiple issues.

由于这是我的示例项目,至少到现在为止还只有模拟数据,所以我决定继续尝试,不要破坏自动化脚本。我愿意在此阶段丢失一些模拟数据,而不是浪费时间。

Since this is a sample project for me with only mock data as of now at least, I decided to go for it and not break the automated scripts. I am willing to lose some mock data at this stage rather than wasting time on it.

如果这是在生产数据库中,我将非常小心地手动创建相同的结果,同时修改支架和迁移文件。

If this were in a production database I would be extremely careful to manually create the same result with intervention modifying both the scaffold and the migration file.

我接受这个答案(基本上说,就目前所知,目前的EF Core不支持它),因为现在没有其他候选人了-我将非常高兴接受是否有更好的答案。

I am accepting this one as an answer (basically saying current EF Core does not support it to the best of my current knowledge) since there is no other candidate now - I will be more than glad to accept if any better answer shows up.

这篇关于EF Core Migrations手动编辑可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆