From e94739adaddc39c3c0bb574c3b3dffce8ec7779d Mon Sep 17 00:00:00 2001 From: Brice Lambson Date: Tue, 21 Sep 2021 14:11:41 -0700 Subject: [PATCH] Tools: Warn/error when using new features on old runtimes Fixes #25153 --- src/ef/Commands/DbContextOptimizeCommand.cs | 8 ++++++++ src/ef/Commands/MigrationsListCommand.cs | 6 ++++++ src/ef/Properties/Resources.Designer.cs | 6 ++++++ src/ef/Properties/Resources.resx | 3 +++ 4 files changed, 23 insertions(+) diff --git a/src/ef/Commands/DbContextOptimizeCommand.cs b/src/ef/Commands/DbContextOptimizeCommand.cs index a34c2afa563..c1ddccdf27e 100644 --- a/src/ef/Commands/DbContextOptimizeCommand.cs +++ b/src/ef/Commands/DbContextOptimizeCommand.cs @@ -1,6 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Microsoft.EntityFrameworkCore.Internal; +using Microsoft.EntityFrameworkCore.Tools.Properties; + namespace Microsoft.EntityFrameworkCore.Tools.Commands { // ReSharper disable once ArrangeTypeModifiers @@ -8,6 +11,11 @@ internal partial class DbContextOptimizeCommand { protected override int Execute(string[] args) { + if (new SemanticVersionComparer().Compare(EFCoreVersion, "6.0.0") < 0) + { + throw new CommandException(Resources.VersionRequired("6.0.0")); + } + using var executor = CreateExecutor(args); executor.OptimizeContext( _outputDir!.Value(), diff --git a/src/ef/Commands/MigrationsListCommand.cs b/src/ef/Commands/MigrationsListCommand.cs index 751b3a0e6fb..f832717ac37 100644 --- a/src/ef/Commands/MigrationsListCommand.cs +++ b/src/ef/Commands/MigrationsListCommand.cs @@ -60,12 +60,14 @@ private static void ReportJsonResults(IReadOnlyList migrations) private static void ReportResults(IEnumerable migrations) { + var anyUnknown = false; var any = false; foreach (var migration in migrations) { var id = migration["Id"] as string; var applied = migration["Applied"] as bool?; Reporter.WriteData($"{id}{(applied != false ? null : Resources.Pending)}"); + anyUnknown |= !applied.HasValue; any = true; } @@ -73,6 +75,10 @@ private static void ReportResults(IEnumerable migrations) { Reporter.WriteInformation(Resources.NoMigrations); } + else if (anyUnknown) + { + Reporter.WriteWarning(Resources.PendingUnknown); + } } } } diff --git a/src/ef/Properties/Resources.Designer.cs b/src/ef/Properties/Resources.Designer.cs index d546df8e610..9e7775be902 100644 --- a/src/ef/Properties/Resources.Designer.cs +++ b/src/ef/Properties/Resources.Designer.cs @@ -449,6 +449,12 @@ public static string OutputDirDescription public static string Pending => GetString("Pending"); + /// + /// Pending status not shown. Unable to determine which migrations have been applied. This can happen when your project uses a version of Entity Framework Core lower than 5.0.0 or when an error occurrs while accessing the database. + /// + public static string PendingUnknown + => GetString("PendingUnknown"); + /// /// Prefix output with level. /// diff --git a/src/ef/Properties/Resources.resx b/src/ef/Properties/Resources.resx index 19467b0bca6..b29d432e76e 100644 --- a/src/ef/Properties/Resources.resx +++ b/src/ef/Properties/Resources.resx @@ -321,6 +321,9 @@ (Pending) + + Pending status not shown. Unable to determine which migrations have been applied. This can happen when your project uses a version of Entity Framework Core lower than 5.0.0 or when an error occurrs while accessing the database. + Prefix output with level.