From 49328ecdba918e5d801ce20eb460ae5b33ebf58b Mon Sep 17 00:00:00 2001 From: Kev Ritchie <65573253+KevRitchie@users.noreply.github.com> Date: Fri, 20 May 2022 00:45:02 +0100 Subject: [PATCH] Add Migration name check Add a check during the Validate method of MigrationsAddCommand to throw a CommandException when someone tries to call their migration "migration". Fixes #23222 --- .../Migrations/Design/MigrationsScaffolder.cs | 10 ++++++++++ src/EFCore.Design/Properties/DesignStrings.Designer.cs | 6 ++++++ src/EFCore.Design/Properties/DesignStrings.resx | 3 +++ src/ef/Commands/MigrationsAddCommand.cs | 1 + 4 files changed, 20 insertions(+) diff --git a/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs b/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs index cead872c3ff..18ae0f012d6 100644 --- a/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs +++ b/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs @@ -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 = diff --git a/src/EFCore.Design/Properties/DesignStrings.Designer.cs b/src/EFCore.Design/Properties/DesignStrings.Designer.cs index 84c4d344d98..e2efe686d93 100644 --- a/src/EFCore.Design/Properties/DesignStrings.Designer.cs +++ b/src/EFCore.Design/Properties/DesignStrings.Designer.cs @@ -783,6 +783,12 @@ public static string WritingSnapshot(object? file) GetString("WritingSnapshot", nameof(file)), file); + /// + /// You cannot add a migration with the name 'Migration'. + /// + public static string CircularBaseClassDependency + => GetString("CircularBaseClassDependency"); + private static string GetString(string name, params string[] formatterNames) { var value = _resourceManager.GetString(name)!; diff --git a/src/EFCore.Design/Properties/DesignStrings.resx b/src/EFCore.Design/Properties/DesignStrings.resx index c8e89f0c6b7..8ce97bf63c8 100644 --- a/src/EFCore.Design/Properties/DesignStrings.resx +++ b/src/EFCore.Design/Properties/DesignStrings.resx @@ -138,6 +138,9 @@ A type-qualified method call requires an instance identifier, a MethodInfo and no chained calls. + + You cannot add a migration with the name 'Migration'. + 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. diff --git a/src/ef/Commands/MigrationsAddCommand.cs b/src/ef/Commands/MigrationsAddCommand.cs index 7115820216a..7a6da0e2505 100644 --- a/src/ef/Commands/MigrationsAddCommand.cs +++ b/src/ef/Commands/MigrationsAddCommand.cs @@ -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;