Skip to content

Commit

Permalink
Add Migration name check
Browse files Browse the repository at this point in the history
Add a check during the Validate method of MigrationsAddCommand to throw a CommandException when someone tries to call their migration "migration".

Fixes #23222
  • Loading branch information
KevRitchie committed May 19, 2022
1 parent d69ba3c commit 49328ec
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public virtual ScaffoldedMigration ScaffoldMigration(
subNamespace = "Migrations";
}

if (string.Equals(migrationName, "migration", StringComparison.OrdinalIgnoreCase))
{
throw new OperationException(DesignStrings.CircularBaseClassDependency);
}

if (Dependencies.MigrationsAssembly.FindMigrationId(migrationName) != null)
{
throw new OperationException(DesignStrings.DuplicateMigrationName(migrationName));
}

var (key, typeInfo) = Dependencies.MigrationsAssembly.Migrations.LastOrDefault();

var migrationNamespace =
Expand Down
6 changes: 6 additions & 0 deletions src/EFCore.Design/Properties/DesignStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore.Design/Properties/DesignStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
<data name="CannotGenerateTypeQualifiedMethodCall" xml:space="preserve">
<value>A type-qualified method call requires an instance identifier, a MethodInfo and no chained calls.</value>
</data>
<data name="CircularBaseClassDependency" xml:space="preserve">
<value>You cannot add a migration with the name 'Migration'.</value>
</data>
<data name="CompiledModelConstructorBinding" xml:space="preserve">
<value>The entity type '{entityType}' has a custom constructor binding. This is usually caused by using proxies. Compiled model can't be generated, because dynamic proxy types are not supported. If you are not using proxies configure the custom constructor binding in '{customize}' in a partial '{className}' class instead.</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/ef/Commands/MigrationsAddCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using Microsoft.EntityFrameworkCore.Tools.Properties;

Expand Down

0 comments on commit 49328ec

Please sign in to comment.