EF Core迁移更改名称

huangapple go评论53阅读模式
英文:

EF Core migrations change name

问题

同事为迁移命名为:“更改扫描对象上的关系”。
这将产生以下输出警告:

21>C:\[REDACTED]\Migrations\Migrations230202123158_changed relatino on scanned objects.cs(8,26,8,57): 警告 CS8981: 类型名称 'changedrelatinoonscannedobjects' 仅包含小写 ASCII 字符。此类名称可能会被语言保留。
21>C:\[REDACTED]\Migrations\Migrations230202123158_changed relatino on scanned objects.Designer.cs(16,19,16,50): 警告 CS8981: 类型名称 'changedrelatinoonscannedobjects' 仅包含小写 ASCII 字符。此类名称可能会被语言保留。

这些迁移已经存在于数据库中。
如何安全地重命名此迁移以抑制警告消息?迁移已经存在于数据库中。

使用.NET7和EF7。

英文:

A colleague named a migration: changed relatino on scanned objects.
This gives the following output warning:

21>C:\[REDACTED]\Migrations\Migrations230202123158_changed relatino on scanned objects.cs(8,26,8,57): warning CS8981: The type name 'changedrelatinoonscannedobjects' only contains lower-cased ascii characters. Such names may become reserved for the language.
21>C:\[REDACTED]\Migrations\Migrations230202123158_changed relatino on scanned objects.Designer.cs(16,19,16,50): warning CS8981: The type name 'changedrelatinoonscannedobjects' only contains lower-cased ascii characters. Such names may become reserved for the language.

These migrations are already in the database.
How would i go about safely renaming this migration to suppress the warning message? The migration is already in the Db.

Using NET7 with EF7.

答案1

得分: 0

You can rename migration files without being worried about applied migration in DB because EF uses the passing name to MigrationAttribute for MigrationId. If you open 20230202123158_changed relatino on scanned objects.Designer.cs class, it should be decorated with the attribute with such an identifier

[Migration("20230202123158_changed_relatino_on_scanned_objects")]

After renaming files you can safely check if there are any pending migration or not?

var appliedMigrations = dbContext.GetService<IHistoryRepository>()
    .GetAppliedMigrations()
    .Select(m => m.MigrationId);

var total = dbContext.GetService<IMigrationsAssembly>()
    .Migrations.Select(m => m.Key);

var hasPendingMigration = total.Except(appliedMigrations).Any();

If there wasn't any update, you're done; otherwise, you should manually update migration id in DB.

英文:

You can rename migration files without being worried about applied migration in DB because EF uses the passing name to MigrationAttribute for MigrationId. If you open 20230202123158_changed relatino on scanned objects.Designer.cs class, it should be decorated with the attribute with such a identifier

[Migration("20230202123158_changed_relatino_on_scanned_objects")]

After renaming files you can safely check if there are any pending migration or not?

var appliedMigrations = dbContext.GetService<IHistoryRepository>()
    .GetAppliedMigrations()
    .Select(m => m.MigrationId);

var total = dbContext.GetService<IMigrationsAssembly>()
    .Migrations.Select(m => m.Key);

var hasPendingMigration = total.Except(appliedMigrations).Any();

If there wasn't any update, you're done otherwise you should manually update migration id in DB.

huangapple
  • 本文由 发表于 2023年3月9日 16:21:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定