From 23ba4f28849a883028284af622eff1dfe6f8bef0 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Sun, 20 Sep 2020 18:20:11 +0300 Subject: [PATCH] Enable NetAnalyzers reliability and globalization rules The following rules led to code changes: - CA2000: Dispose objects before losing scope - CA2012: Use ValueTasks correctly - CA2008: Do not create tasks without passing a TaskScheduler - CA2016: Forward the 'CancellationToken' parameter to methods that take one - CA1310: Specify StringComparison for correctness - CA1309: Use ordinal stringcomparison - CA1304: Specify CultureInfo Part of #18870 --- rulesets/FxCop.ruleset | 2 +- src/.editorconfig | 68 +++++++++++++++++++ src/Directory.Build.props | 1 + .../InternalUsageDiagnosticAnalyzer.cs | 2 +- ...yableMethodTranslatingExpressionVisitor.cs | 3 +- ...ingExpressionVisitor.QueryingEnumerable.cs | 9 ++- .../Query/Internal/KeyAccessExpression.cs | 2 +- .../Query/Internal/ProjectionExpression.cs | 5 +- .../Query/Internal/RootReferenceExpression.cs | 2 +- .../Query/Internal/SqlFunctionExpression.cs | 2 +- .../Query/Internal/SqlParameterExpression.cs | 3 +- .../Storage/Internal/CosmosClientWrapper.cs | 4 +- .../Extensions/RelationalTaskExtensions.cs | 64 ----------------- .../Internal/MigrationsModelDiffer.cs | 6 +- .../Query/QuerySqlGenerator.cs | 5 +- ...yableMethodTranslatingExpressionVisitor.cs | 4 +- .../Query/SqlExpressionFactory.cs | 2 +- .../Query/SqlExpressions/ColumnExpression.cs | 2 +- .../Query/SqlExpressions/FromSqlExpression.cs | 2 +- .../SqlExpressions/ProjectionExpression.cs | 7 +- .../SqlExpressions/SqlFragmentExpression.cs | 4 +- .../SqlExpressions/SqlFunctionExpression.cs | 4 +- .../SqlExpressions/SqlParameterExpression.cs | 2 +- .../Query/SqlExpressions/TableExpression.cs | 2 +- .../SqlExpressions/TableExpressionBase.cs | 2 +- .../TableValuedFunctionExpression.cs | 2 +- .../Storage/RelationalConnection.cs | 3 +- .../SqlServerLineStringMemberTranslator.cs | 4 +- .../SqlServerAnnotationCodeGenerator.cs | 2 +- ...rchConditionConvertingExpressionVisitor.cs | 3 +- .../SqlServerDateTimeMethodTranslator.cs | 4 +- .../SqliteDateTimeMemberTranslator.cs | 4 +- .../Query/Internal/SqliteMathTranslator.cs | 3 +- .../Storage/Internal/SqliteGuidTypeMapping.cs | 2 +- .../Diagnostics/CoreLoggerExtensions.cs | 2 +- .../Extensions/Internal/TypeExtensions.cs | 2 +- src/EFCore/Query/Internal/EntityQueryable`.cs | 4 +- .../NavigationExpandingExpressionVisitor.cs | 6 +- ...yableMethodNormalizingExpressionVisitor.cs | 2 +- .../Storage/ExecutionStrategyExtensions.cs | 2 +- .../SqliteValueBinder.cs | 2 +- src/dotnet-ef/RootCommand.cs | 17 +++-- .../CommandLineApplication.cs | 10 +-- .../CommandLineApplicationExtensions.cs | 4 +- src/ef/CommandLineUtils/CommandOption.cs | 12 ++-- src/ef/Commands/DatabaseDropCommand.cs | 2 +- src/ef/Commands/DatabaseUpdateCommand.cs | 4 +- src/ef/Commands/DbContextInfoCommand.cs | 3 +- src/ef/Commands/DbContextListCommand.cs | 3 +- src/ef/Commands/DbContextScaffoldCommand.cs | 3 +- src/ef/Commands/DbContextScriptCommand.cs | 3 +- src/ef/Commands/MigrationsAddCommand.cs | 4 +- src/ef/Commands/MigrationsListCommand.cs | 4 +- src/ef/Commands/MigrationsRemoveCommand.cs | 5 +- src/ef/Commands/MigrationsScriptCommand.cs | 4 +- 55 files changed, 170 insertions(+), 164 deletions(-) create mode 100644 src/.editorconfig delete mode 100644 src/EFCore.Relational/Extensions/RelationalTaskExtensions.cs diff --git a/rulesets/FxCop.ruleset b/rulesets/FxCop.ruleset index 8270cc41534..cc76efd47f3 100644 --- a/rulesets/FxCop.ruleset +++ b/rulesets/FxCop.ruleset @@ -95,7 +95,7 @@ - + diff --git a/src/.editorconfig b/src/.editorconfig new file mode 100644 index 00000000000..dbffb65a01b --- /dev/null +++ b/src/.editorconfig @@ -0,0 +1,68 @@ +# Code analysis rules and configuration + +root = false + +# Code files +[*.cs] + +# Globalization rules + +# CA1303: Do not pass literals as localized parameters +dotnet_diagnostic.CA1303.severity = none + +# CA1304: Specify CultureInfo +dotnet_diagnostic.CA1304.severity = error + +# CA1305: Specify IFormatProvider +dotnet_diagnostic.CA1305.severity = none + +# CA1307: Specify StringComparison for clarity +dotnet_diagnostic.CA1307.severity = none + +# CA1308: Normalize strings to uppercase +dotnet_diagnostic.CA1308.severity = none + +# CA1309: Use ordinal stringcomparison +dotnet_diagnostic.CA1309.severity = error + +# CA1310: Specify StringComparison for correctness +dotnet_diagnostic.CA1310.severity = error + +# CA2101: Specify marshaling for P/Invoke string arguments +dotnet_diagnostic.CA2101.severity = error + +# Reliability Rules + +# CA2000: Dispose objects before losing scope +# Not reliable enough - false positives +dotnet_diagnostic.CA2000.severity = suggestion + +# CA2002: Do not lock on objects with weak identity +dotnet_diagnostic.CA2002.severity = error + +# CA2007: Consider calling ConfigureAwait on the awaited task +dotnet_diagnostic.CA2007.severity = error + +# CA2008: Do not create tasks without passing a TaskScheduler +dotnet_diagnostic.CA2008.severity = error + +# CA2009: Do not call ToImmutableCollection on an ImmutableCollection value +dotnet_diagnostic.CA2009.severity = error + +# CA2011: Avoid infinite recursion +dotnet_diagnostic.CA2011.severity = error + +# CA2012: Use ValueTasks correctly +dotnet_diagnostic.CA2012.severity = error + +# CA2013: Do not use ReferenceEquals with value types +dotnet_diagnostic.CA2013.severity = error + +# CA2014: Do not use stackalloc in loops +dotnet_diagnostic.CA2014.severity = error + +# CA2015: Do not define finalizers for types derived from MemoryManager +dotnet_diagnostic.CA2015.severity = error + +# CA2016: Forward the 'CancellationToken' parameter to methods that take one +dotnet_diagnostic.CA2016.severity = error diff --git a/src/Directory.Build.props b/src/Directory.Build.props index f6f0e7c55a2..a8bb7415546 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,6 +2,7 @@ + latest True True $(MSBuildThisFileDirectory)..\rulesets\EFCore.ruleset diff --git a/src/EFCore.Analyzers/InternalUsageDiagnosticAnalyzer.cs b/src/EFCore.Analyzers/InternalUsageDiagnosticAnalyzer.cs index 5fe9f49e205..3ece2c37c2f 100644 --- a/src/EFCore.Analyzers/InternalUsageDiagnosticAnalyzer.cs +++ b/src/EFCore.Analyzers/InternalUsageDiagnosticAnalyzer.cs @@ -325,7 +325,7 @@ private static bool IsInInternalNamespace(ISymbol symbol) { if (symbol?.ContainingNamespace?.ToDisplayString() is string ns) { - var i = ns.IndexOf("EntityFrameworkCore"); + var i = ns.IndexOf("EntityFrameworkCore", StringComparison.Ordinal); return i != -1 diff --git a/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs index a5860d137cb..5bf2882b31d 100644 --- a/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs @@ -1159,7 +1159,8 @@ Expression GetPartitionKeyValue(BinaryExpression binaryExpression, IEntityType e if (valueExpression is ConstantExpression || (valueExpression is ParameterExpression valueParameterExpression - && valueParameterExpression.Name?.StartsWith(QueryCompilationContext.QueryParameterPrefix) == true)) + && valueParameterExpression.Name? + .StartsWith(QueryCompilationContext.QueryParameterPrefix, StringComparison.Ordinal) == true)) { return valueExpression; } diff --git a/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs b/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs index 05157db529e..b02de740a51 100644 --- a/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs +++ b/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs @@ -252,9 +252,12 @@ public async ValueTask MoveNextAsync() public ValueTask DisposeAsync() { - _enumerator?.DisposeAsync(); - _enumerator = null; - + var enumerator = _enumerator; + if (enumerator != null) + { + _enumerator = null; + return enumerator.DisposeAsync(); + } return default; } } diff --git a/src/EFCore.Cosmos/Query/Internal/KeyAccessExpression.cs b/src/EFCore.Cosmos/Query/Internal/KeyAccessExpression.cs index 431f8e22789..58fff4cd049 100644 --- a/src/EFCore.Cosmos/Query/Internal/KeyAccessExpression.cs +++ b/src/EFCore.Cosmos/Query/Internal/KeyAccessExpression.cs @@ -122,7 +122,7 @@ public override bool Equals(object obj) private bool Equals(KeyAccessExpression keyAccessExpression) => base.Equals(keyAccessExpression) - && string.Equals(Name, keyAccessExpression.Name) + && Name == keyAccessExpression.Name && AccessExpression.Equals(keyAccessExpression.AccessExpression); /// diff --git a/src/EFCore.Cosmos/Query/Internal/ProjectionExpression.cs b/src/EFCore.Cosmos/Query/Internal/ProjectionExpression.cs index c2d029fb525..1d7629213b7 100644 --- a/src/EFCore.Cosmos/Query/Internal/ProjectionExpression.cs +++ b/src/EFCore.Cosmos/Query/Internal/ProjectionExpression.cs @@ -107,8 +107,7 @@ void IPrintableExpression.Print(ExpressionPrinter expressionPrinter) Check.NotNull(expressionPrinter, nameof(expressionPrinter)); expressionPrinter.Visit(Expression); - if (!string.Equals(string.Empty, Alias) - && !string.Equals(Alias, Name)) + if (Alias != string.Empty && Alias != Name) { expressionPrinter.Append(" AS " + Alias); } @@ -127,7 +126,7 @@ public override bool Equals(object obj) && Equals(projectionExpression)); private bool Equals(ProjectionExpression projectionExpression) - => string.Equals(Alias, projectionExpression.Alias) + => Alias == projectionExpression.Alias && Expression.Equals(projectionExpression.Expression); /// diff --git a/src/EFCore.Cosmos/Query/Internal/RootReferenceExpression.cs b/src/EFCore.Cosmos/Query/Internal/RootReferenceExpression.cs index 7ebf7f23dbf..701bddf2ef8 100644 --- a/src/EFCore.Cosmos/Query/Internal/RootReferenceExpression.cs +++ b/src/EFCore.Cosmos/Query/Internal/RootReferenceExpression.cs @@ -107,7 +107,7 @@ public override bool Equals(object obj) && Equals(rootReferenceExpression)); private bool Equals(RootReferenceExpression rootReferenceExpression) - => string.Equals(Alias, rootReferenceExpression.Alias) + => Alias == rootReferenceExpression.Alias && EntityType.Equals(rootReferenceExpression.EntityType); /// diff --git a/src/EFCore.Cosmos/Query/Internal/SqlFunctionExpression.cs b/src/EFCore.Cosmos/Query/Internal/SqlFunctionExpression.cs index 7510757fe05..2527acb2ba3 100644 --- a/src/EFCore.Cosmos/Query/Internal/SqlFunctionExpression.cs +++ b/src/EFCore.Cosmos/Query/Internal/SqlFunctionExpression.cs @@ -134,7 +134,7 @@ public override bool Equals(object obj) private bool Equals(SqlFunctionExpression sqlFunctionExpression) => base.Equals(sqlFunctionExpression) - && string.Equals(Name, sqlFunctionExpression.Name) + && Name == sqlFunctionExpression.Name && Arguments.SequenceEqual(sqlFunctionExpression.Arguments); /// diff --git a/src/EFCore.Cosmos/Query/Internal/SqlParameterExpression.cs b/src/EFCore.Cosmos/Query/Internal/SqlParameterExpression.cs index 3dde9f42b70..98196b9d3c4 100644 --- a/src/EFCore.Cosmos/Query/Internal/SqlParameterExpression.cs +++ b/src/EFCore.Cosmos/Query/Internal/SqlParameterExpression.cs @@ -89,8 +89,7 @@ public override bool Equals(object obj) && Equals(sqlParameterExpression)); private bool Equals(SqlParameterExpression sqlParameterExpression) - => base.Equals(sqlParameterExpression) - && string.Equals(Name, sqlParameterExpression.Name); + => base.Equals(sqlParameterExpression) && Name != sqlParameterExpression.Name; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to diff --git a/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs b/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs index c59d65b6749..1342182e7a2 100644 --- a/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs +++ b/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs @@ -565,8 +565,8 @@ private static JObject JObjectFromReadItemResponseMessage(ResponseMessage respon responseMessage.EnsureSuccessStatusCode(); var responseStream = responseMessage.Content; - var reader = new StreamReader(responseStream); - var jsonReader = new JsonTextReader(reader); + using var reader = new StreamReader(responseStream); + using var jsonReader = new JsonTextReader(reader); var jObject = Serializer.Deserialize(jsonReader); diff --git a/src/EFCore.Relational/Extensions/RelationalTaskExtensions.cs b/src/EFCore.Relational/Extensions/RelationalTaskExtensions.cs deleted file mode 100644 index 2706d1f0d90..00000000000 --- a/src/EFCore.Relational/Extensions/RelationalTaskExtensions.cs +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -// ReSharper disable once CheckNamespace - -namespace System.Threading.Tasks -{ - internal static class RelationalTaskExtensions - { - public static Task Cast(this Task task) - where TDerived : T - { - var taskCompletionSource = new TaskCompletionSource(); - - task.ContinueWith( - t => - { - if (t.IsFaulted) - { - // ReSharper disable once PossibleNullReferenceException - taskCompletionSource.TrySetException(t.Exception.InnerExceptions); - } - else if (t.IsCanceled) - { - taskCompletionSource.TrySetCanceled(); - } - else - { - taskCompletionSource.TrySetResult((TDerived)t.Result); - } - }, - TaskContinuationOptions.ExecuteSynchronously); - - return taskCompletionSource.Task; - } - - public static Task CastToNullable(this Task task) - where T : struct - { - var taskCompletionSource = new TaskCompletionSource(); - - task.ContinueWith( - t => - { - if (t.IsFaulted) - { - // ReSharper disable once PossibleNullReferenceException - taskCompletionSource.TrySetException(t.Exception.InnerExceptions); - } - else if (t.IsCanceled) - { - taskCompletionSource.TrySetCanceled(); - } - else - { - taskCompletionSource.TrySetResult(t.Result); - } - }, - TaskContinuationOptions.ExecuteSynchronously); - - return taskCompletionSource.Task; - } - } -} diff --git a/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs b/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs index ed91236f62e..d5a6d5bd30c 100644 --- a/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs +++ b/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs @@ -942,14 +942,12 @@ private static bool EntityTypePathEquals(IEntityType source, IEntityType target, return true; } - if (!string.Equals(source.Name, target.Name)) + if (source.Name != target.Name) { return false; } - if (!string.Equals( - GetDefiningNavigationName(source), - GetDefiningNavigationName(target))) + if (GetDefiningNavigationName(source) != GetDefiningNavigationName(target)) { return false; } diff --git a/src/EFCore.Relational/Query/QuerySqlGenerator.cs b/src/EFCore.Relational/Query/QuerySqlGenerator.cs index 0abb1f29936..8018d64af5e 100644 --- a/src/EFCore.Relational/Query/QuerySqlGenerator.cs +++ b/src/EFCore.Relational/Query/QuerySqlGenerator.cs @@ -249,9 +249,8 @@ protected override Expression VisitProjection(ProjectionExpression projectionExp Visit(projectionExpression.Expression); - if (!string.Equals(string.Empty, projectionExpression.Alias) - && !(projectionExpression.Expression is ColumnExpression column - && string.Equals(column.Name, projectionExpression.Alias))) + if (projectionExpression.Alias != string.Empty + && !(projectionExpression.Expression is ColumnExpression column && column.Name == projectionExpression.Alias)) { _relationalCommandBuilder.Append(AliasSeparator + _sqlGenerationHelper.DelimitIdentifier(projectionExpression.Alias)); } diff --git a/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs index cf9561c1061..2ff0452beab 100644 --- a/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs @@ -93,7 +93,7 @@ protected override Expression VisitExtension(Expression extensionExpression) fromSqlQueryRootExpression.EntityType, new FromSqlExpression( fromSqlQueryRootExpression.EntityType.GetDefaultMappings().SingleOrDefault().Table.Name.Substring(0, 1) - .ToLower(), + .ToLowerInvariant(), fromSqlQueryRootExpression.Sql, fromSqlQueryRootExpression.Argument))); @@ -1461,7 +1461,7 @@ private static IDictionary GetPropertyExpressionFro tableExpression = selectExpression.Tables .Select(t => (t as InnerJoinExpression)?.Table ?? (t as LeftJoinExpression)?.Table ?? t) .Cast() - .First(t => string.Equals(t.Name, table.Name) && string.Equals(t.Schema, table.Schema)); + .First(t => t.Name == table.Name && t.Schema == table.Schema); } var propertyExpressions = new Dictionary(); diff --git a/src/EFCore.Relational/Query/SqlExpressionFactory.cs b/src/EFCore.Relational/Query/SqlExpressionFactory.cs index 2187d5ff070..7ec38bd5960 100644 --- a/src/EFCore.Relational/Query/SqlExpressionFactory.cs +++ b/src/EFCore.Relational/Query/SqlExpressionFactory.cs @@ -817,7 +817,7 @@ public virtual SelectExpression Select(IEntityType entityType, string sql, Expre Check.NotNull(sql, nameof(sql)); var tableExpression = new FromSqlExpression( - entityType.GetDefaultMappings().SingleOrDefault().Table.Name.Substring(0, 1).ToLower(), sql, sqlArguments); + entityType.GetDefaultMappings().SingleOrDefault().Table.Name.Substring(0, 1).ToLowerInvariant(), sql, sqlArguments); var selectExpression = new SelectExpression(entityType, tableExpression); AddConditions(selectExpression, entityType); diff --git a/src/EFCore.Relational/Query/SqlExpressions/ColumnExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/ColumnExpression.cs index 2f79b64d180..84783d07f19 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/ColumnExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/ColumnExpression.cs @@ -114,7 +114,7 @@ public override bool Equals(object obj) private bool Equals(ColumnExpression columnExpression) => base.Equals(columnExpression) - && string.Equals(Name, columnExpression.Name) + && Name == columnExpression.Name && Table.Equals(columnExpression.Table) && IsNullable == columnExpression.IsNullable; diff --git a/src/EFCore.Relational/Query/SqlExpressions/FromSqlExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/FromSqlExpression.cs index 0970c7e831f..29935de2659 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/FromSqlExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/FromSqlExpression.cs @@ -102,7 +102,7 @@ public override bool Equals(object obj) private bool Equals(FromSqlExpression fromSqlExpression) => base.Equals(fromSqlExpression) - && string.Equals(Sql, fromSqlExpression.Sql) + && Sql == fromSqlExpression.Sql && ExpressionEqualityComparer.Instance.Equals(Arguments, fromSqlExpression.Arguments); /// diff --git a/src/EFCore.Relational/Query/SqlExpressions/ProjectionExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/ProjectionExpression.cs index 66e7231b64b..00b778a4146 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/ProjectionExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/ProjectionExpression.cs @@ -76,9 +76,9 @@ void IPrintableExpression.Print(ExpressionPrinter expressionPrinter) Check.NotNull(expressionPrinter, nameof(expressionPrinter)); expressionPrinter.Visit(Expression); - if (!string.Equals(string.Empty, Alias) + if (Alias != string.Empty && !(Expression is ColumnExpression column - && string.Equals(column.Name, Alias))) + && column.Name == Alias)) { expressionPrinter.Append(" AS " + Alias); } @@ -92,8 +92,7 @@ public override bool Equals(object obj) && Equals(projectionExpression)); private bool Equals(ProjectionExpression projectionExpression) - => string.Equals(Alias, projectionExpression.Alias) - && Expression.Equals(projectionExpression.Expression); + => Alias == projectionExpression.Alias && Expression.Equals(projectionExpression.Expression); /// public override int GetHashCode() diff --git a/src/EFCore.Relational/Query/SqlExpressions/SqlFragmentExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SqlFragmentExpression.cs index e1959727b18..840cb1f5ac7 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SqlFragmentExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SqlFragmentExpression.cs @@ -61,8 +61,8 @@ public override bool Equals(object obj) private bool Equals(SqlFragmentExpression sqlFragmentExpression) => base.Equals(sqlFragmentExpression) - && string.Equals(Sql, sqlFragmentExpression.Sql) - && !string.Equals(Sql, "*"); // We make star projection different because it could be coming from different table. + && Sql == sqlFragmentExpression.Sql + && Sql != "*"; // We make star projection different because it could be coming from different table. /// public override int GetHashCode() diff --git a/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs index 925be2edf72..e4bd4470f43 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs @@ -379,8 +379,8 @@ public override bool Equals(object obj) private bool Equals(SqlFunctionExpression sqlFunctionExpression) => base.Equals(sqlFunctionExpression) - && string.Equals(Name, sqlFunctionExpression.Name) - && string.Equals(Schema, sqlFunctionExpression.Schema) + && Name == sqlFunctionExpression.Name + && Schema == sqlFunctionExpression.Schema && ((Instance == null && sqlFunctionExpression.Instance == null) || (Instance != null && Instance.Equals(sqlFunctionExpression.Instance))) && Arguments.SequenceEqual(sqlFunctionExpression.Arguments); diff --git a/src/EFCore.Relational/Query/SqlExpressions/SqlParameterExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SqlParameterExpression.cs index 333657171a1..169affa36f8 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SqlParameterExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SqlParameterExpression.cs @@ -74,7 +74,7 @@ public override bool Equals(object obj) private bool Equals(SqlParameterExpression sqlParameterExpression) => base.Equals(sqlParameterExpression) - && string.Equals(Name, sqlParameterExpression.Name); + && Name == sqlParameterExpression.Name; /// public override int GetHashCode() diff --git a/src/EFCore.Relational/Query/SqlExpressions/TableExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/TableExpression.cs index 541521e1039..6ea2c6bbbc2 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/TableExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/TableExpression.cs @@ -21,7 +21,7 @@ namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions public sealed class TableExpression : TableExpressionBase { internal TableExpression([NotNull] ITableBase table) - : base(table.Name.Substring(0, 1).ToLower()) + : base(table.Name.Substring(0, 1).ToLowerInvariant()) { Name = table.Name; Schema = table.Schema; diff --git a/src/EFCore.Relational/Query/SqlExpressions/TableExpressionBase.cs b/src/EFCore.Relational/Query/SqlExpressions/TableExpressionBase.cs index 9d90272b378..cf567ef8829 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/TableExpressionBase.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/TableExpressionBase.cs @@ -69,7 +69,7 @@ public override bool Equals(object obj) && Equals(tableExpressionBase)); private bool Equals(TableExpressionBase tableExpressionBase) - => string.Equals(Alias, tableExpressionBase.Alias); + => Alias == tableExpressionBase.Alias; /// public override int GetHashCode() diff --git a/src/EFCore.Relational/Query/SqlExpressions/TableValuedFunctionExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/TableValuedFunctionExpression.cs index 91f291996fc..38abd4ee0b3 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/TableValuedFunctionExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/TableValuedFunctionExpression.cs @@ -29,7 +29,7 @@ public class TableValuedFunctionExpression : TableExpressionBase /// The arguments of the function. public TableValuedFunctionExpression([NotNull] IStoreFunction storeFunction, [NotNull] IReadOnlyList arguments) : this( - storeFunction.Name.Substring(0, 1).ToLower(), + storeFunction.Name.Substring(0, 1).ToLowerInvariant(), Check.NotNull(storeFunction, nameof(storeFunction)), Check.NotNull(arguments, nameof(arguments))) { diff --git a/src/EFCore.Relational/Storage/RelationalConnection.cs b/src/EFCore.Relational/Storage/RelationalConnection.cs index dc1d7e61028..b1136ec41d2 100644 --- a/src/EFCore.Relational/Storage/RelationalConnection.cs +++ b/src/EFCore.Relational/Storage/RelationalConnection.cs @@ -111,8 +111,7 @@ public virtual string ConnectionString get => _connectionString ?? _connection?.ConnectionString; set { - if (_connection != null - && !string.Equals(_connection.ConnectionString, value, StringComparison.InvariantCulture)) + if (_connection != null && _connection.ConnectionString != value) { // Let ADO.NET throw if this is not valid for the state of the connection. _connection.ConnectionString = value; diff --git a/src/EFCore.SqlServer.NTS/Query/Internal/SqlServerLineStringMemberTranslator.cs b/src/EFCore.SqlServer.NTS/Query/Internal/SqlServerLineStringMemberTranslator.cs index ed28212410b..ecd134dd587 100644 --- a/src/EFCore.SqlServer.NTS/Query/Internal/SqlServerLineStringMemberTranslator.cs +++ b/src/EFCore.SqlServer.NTS/Query/Internal/SqlServerLineStringMemberTranslator.cs @@ -69,9 +69,9 @@ public virtual SqlExpression Translate( { Check.DebugAssert(instance.TypeMapping != null, "Instance must have typeMapping assigned."); var storeType = instance.TypeMapping.StoreType; - var isGeography = string.Equals(storeType, "geography", StringComparison.OrdinalIgnoreCase); + var isGeography = storeType == "geography"; - if (isGeography && string.Equals(functionName, "STIsRing")) + if (isGeography && functionName == "STIsRing") { return null; } diff --git a/src/EFCore.SqlServer/Design/Internal/SqlServerAnnotationCodeGenerator.cs b/src/EFCore.SqlServer/Design/Internal/SqlServerAnnotationCodeGenerator.cs index a0dbfa7cdbd..cfbcfb13138 100644 --- a/src/EFCore.SqlServer/Design/Internal/SqlServerAnnotationCodeGenerator.cs +++ b/src/EFCore.SqlServer/Design/Internal/SqlServerAnnotationCodeGenerator.cs @@ -71,7 +71,7 @@ protected override bool IsHandledByConvention(IModel model, IAnnotation annotati if (annotation.Name == RelationalAnnotationNames.DefaultSchema) { - return string.Equals("dbo", (string)annotation.Value); + return (string)annotation.Value == "dbo"; } return annotation.Name == SqlServerAnnotationNames.ValueGenerationStrategy diff --git a/src/EFCore.SqlServer/Query/Internal/SearchConditionConvertingExpressionVisitor.cs b/src/EFCore.SqlServer/Query/Internal/SearchConditionConvertingExpressionVisitor.cs index 53c4deef2aa..c9b4046fd25 100644 --- a/src/EFCore.SqlServer/Query/Internal/SearchConditionConvertingExpressionVisitor.cs +++ b/src/EFCore.SqlServer/Query/Internal/SearchConditionConvertingExpressionVisitor.cs @@ -461,8 +461,7 @@ protected override Expression VisitSqlFunction(SqlFunctionExpression sqlFunction _isSearchCondition = parentSearchCondition; var newFunction = sqlFunctionExpression.Update(instance, arguments); - var condition = string.Equals(sqlFunctionExpression.Name, "FREETEXT") - || string.Equals(sqlFunctionExpression.Name, "CONTAINS"); + var condition = sqlFunctionExpression.Name == "FREETEXT" || sqlFunctionExpression.Name == "CONTAINS"; return ApplyConversion(newFunction, condition); } diff --git a/src/EFCore.SqlServer/Query/Internal/SqlServerDateTimeMethodTranslator.cs b/src/EFCore.SqlServer/Query/Internal/SqlServerDateTimeMethodTranslator.cs index 15ab95f43b1..cf1c1755212 100644 --- a/src/EFCore.SqlServer/Query/Internal/SqlServerDateTimeMethodTranslator.cs +++ b/src/EFCore.SqlServer/Query/Internal/SqlServerDateTimeMethodTranslator.cs @@ -71,8 +71,8 @@ public virtual SqlExpression Translate( { // DateAdd does not accept number argument outside of int range // AddYears/AddMonths take int argument so no need to check for range - return !datePart.Equals("year") - && !datePart.Equals("month") + return datePart != "year" + && datePart != "month" && arguments[0] is SqlConstantExpression sqlConstant && ((double)sqlConstant.Value >= int.MaxValue || (double)sqlConstant.Value <= int.MinValue) diff --git a/src/EFCore.Sqlite.Core/Query/Internal/SqliteDateTimeMemberTranslator.cs b/src/EFCore.Sqlite.Core/Query/Internal/SqliteDateTimeMemberTranslator.cs index 366ded3b78a..57c1a8152ba 100644 --- a/src/EFCore.Sqlite.Core/Query/Internal/SqliteDateTimeMemberTranslator.cs +++ b/src/EFCore.Sqlite.Core/Query/Internal/SqliteDateTimeMemberTranslator.cs @@ -77,7 +77,7 @@ public virtual SqlExpression Translate( returnType); } - if (string.Equals(memberName, nameof(DateTime.Ticks))) + if (memberName == nameof(DateTime.Ticks)) { return _sqlExpressionFactory.Convert( _sqlExpressionFactory.Multiply( @@ -93,7 +93,7 @@ public virtual SqlExpression Translate( typeof(long)); } - if (string.Equals(memberName, nameof(DateTime.Millisecond))) + if (memberName == nameof(DateTime.Millisecond)) { return _sqlExpressionFactory.Modulo( _sqlExpressionFactory.Multiply( diff --git a/src/EFCore.Sqlite.Core/Query/Internal/SqliteMathTranslator.cs b/src/EFCore.Sqlite.Core/Query/Internal/SqliteMathTranslator.cs index 2da77f952df..4af5643736a 100644 --- a/src/EFCore.Sqlite.Core/Query/Internal/SqliteMathTranslator.cs +++ b/src/EFCore.Sqlite.Core/Query/Internal/SqliteMathTranslator.cs @@ -85,8 +85,7 @@ public virtual SqlExpression Translate( { RelationalTypeMapping typeMapping; List newArguments = null; - if (string.Equals(sqlFunctionName, "max") - || string.Equals(sqlFunctionName, "max")) + if (sqlFunctionName == "max" || sqlFunctionName == "max") { typeMapping = ExpressionExtensions.InferTypeMapping(arguments[0], arguments[1]); newArguments = new List diff --git a/src/EFCore.Sqlite.Core/Storage/Internal/SqliteGuidTypeMapping.cs b/src/EFCore.Sqlite.Core/Storage/Internal/SqliteGuidTypeMapping.cs index 9fab377de5d..8240443ebb7 100644 --- a/src/EFCore.Sqlite.Core/Storage/Internal/SqliteGuidTypeMapping.cs +++ b/src/EFCore.Sqlite.Core/Storage/Internal/SqliteGuidTypeMapping.cs @@ -55,6 +55,6 @@ protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters p /// doing so can result in application failures when updating to a new Entity Framework Core release. /// protected override string GenerateNonNullSqlLiteral(object value) - => "'" + ((Guid)value).ToString().ToUpper() + "'"; + => "'" + ((Guid)value).ToString().ToUpperInvariant() + "'"; } } diff --git a/src/EFCore/Diagnostics/CoreLoggerExtensions.cs b/src/EFCore/Diagnostics/CoreLoggerExtensions.cs index d8c1c240a7b..904ac5295d0 100644 --- a/src/EFCore/Diagnostics/CoreLoggerExtensions.cs +++ b/src/EFCore/Diagnostics/CoreLoggerExtensions.cs @@ -737,7 +737,7 @@ private static string GenerateDebugInfoString( { if (newDebugInfo.TryGetValue(key, out var value)) { - if (!value.Equals(cachedDebugInfo[key])) + if (value != cachedDebugInfo[key]) { conflicts.Add(CoreStrings.ServiceProviderConfigChanged(key)); } diff --git a/src/EFCore/Extensions/Internal/TypeExtensions.cs b/src/EFCore/Extensions/Internal/TypeExtensions.cs index 48112e7e26a..e2a7fb491da 100644 --- a/src/EFCore/Extensions/Internal/TypeExtensions.cs +++ b/src/EFCore/Extensions/Internal/TypeExtensions.cs @@ -47,7 +47,7 @@ public static string GenerateParameterName([NotNull] this Type type) var sb = new StringBuilder(); var removeLowerCase = sb.Append(type.Name.Where(char.IsUpper).ToArray()).ToString(); - return removeLowerCase.Length > 0 ? removeLowerCase.ToLower() : type.Name.ToLower().Substring(0, 1); + return removeLowerCase.Length > 0 ? removeLowerCase.ToLowerInvariant() : type.Name.ToLowerInvariant().Substring(0, 1); } /// diff --git a/src/EFCore/Query/Internal/EntityQueryable`.cs b/src/EFCore/Query/Internal/EntityQueryable`.cs index 6b11bce7d4a..d5e48862357 100644 --- a/src/EFCore/Query/Internal/EntityQueryable`.cs +++ b/src/EFCore/Query/Internal/EntityQueryable`.cs @@ -106,7 +106,9 @@ IEnumerator IEnumerable.GetEnumerator() /// doing so can result in application failures when updating to a new Entity Framework Core release. /// public virtual IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) - => _queryProvider.ExecuteAsync>(Expression).GetAsyncEnumerator(cancellationToken); + => _queryProvider + .ExecuteAsync>(Expression, cancellationToken) + .GetAsyncEnumerator(cancellationToken); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to diff --git a/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs b/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs index b5d7b6f5e60..c8199807251 100644 --- a/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs +++ b/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs @@ -457,9 +457,7 @@ when QueryableMethods.IsSumWithSelector(method): return ProcessInclude( source, methodCallExpression.Arguments[1], - string.Equals( - method.Name, - nameof(EntityFrameworkQueryableExtensions.ThenInclude))); + method.Name == nameof(EntityFrameworkQueryableExtensions.ThenInclude)); case nameof(Queryable.GroupBy) when genericMethod == QueryableMethods.GroupByWithKeySelector: @@ -1603,7 +1601,7 @@ private NavigationExpansionExpression CreateNavigationExpansionExpression( PopulateEagerLoadedNavigations(entityReference.IncludePaths); var currentTree = new NavigationTreeExpression(entityReference); - var parameterName = GetParameterName(entityType.ShortName()[0].ToString().ToLower()); + var parameterName = GetParameterName(entityType.ShortName()[0].ToString().ToLowerInvariant()); return new NavigationExpansionExpression(sourceExpression, currentTree, currentTree, parameterName); } diff --git a/src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs b/src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs index 713dcc4a923..2025e7ce373 100644 --- a/src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs +++ b/src/EFCore/Query/Internal/QueryableMethodNormalizingExpressionVisitor.cs @@ -70,7 +70,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp if (methodCallExpression.Method.DeclaringType.IsGenericType && (methodCallExpression.Method.DeclaringType.GetGenericTypeDefinition() == typeof(ICollection<>) || methodCallExpression.Method.DeclaringType.GetGenericTypeDefinition() == typeof(List<>)) - && string.Equals(nameof(List.Contains), methodCallExpression.Method.Name)) + && methodCallExpression.Method.Name == nameof(List.Contains)) { visitedExpression = TryConvertListContainsToQueryableContains(methodCallExpression); } diff --git a/src/EFCore/Storage/ExecutionStrategyExtensions.cs b/src/EFCore/Storage/ExecutionStrategyExtensions.cs index f6e81f0ac44..40899667ac4 100644 --- a/src/EFCore/Storage/ExecutionStrategyExtensions.cs +++ b/src/EFCore/Storage/ExecutionStrategyExtensions.cs @@ -740,7 +740,7 @@ public static Task ExecuteInTransactionAsync( return s.Result; }, async (c, s, ct) => new ExecutionResult( s.CommitFailed && await s.VerifySucceeded(s.State, ct).ConfigureAwait(false), - s.Result)); + s.Result), cancellationToken); private sealed class ExecutionState { diff --git a/src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs b/src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs index 534c86a62a0..9906f6b20b0 100644 --- a/src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs +++ b/src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs @@ -136,7 +136,7 @@ public virtual void Bind() var guid = (Guid)_value; if (_sqliteType != SqliteType.Blob) { - var value = guid.ToString().ToUpper(); + var value = guid.ToString().ToUpperInvariant(); BindText(value); } else diff --git a/src/dotnet-ef/RootCommand.cs b/src/dotnet-ef/RootCommand.cs index 2e0e5fde90b..7db5d7f42f9 100644 --- a/src/dotnet-ef/RootCommand.cs +++ b/src/dotnet-ef/RootCommand.cs @@ -131,16 +131,15 @@ protected override int Execute(string[] _) if (!string.IsNullOrEmpty(projectAssetsFile)) { - using (var reader = JsonDocument.Parse(File.OpenRead(projectAssetsFile))) + using var file = File.OpenRead(projectAssetsFile); + using var reader = JsonDocument.Parse(file); + var projectAssets = reader.RootElement; + var packageFolders = projectAssets.GetProperty("packageFolders").EnumerateObject().Select(p => p.Name); + + foreach (var packageFolder in packageFolders) { - var projectAssets = reader.RootElement; - var packageFolders = projectAssets.GetProperty("packageFolders").EnumerateObject().Select(p => p.Name); - - foreach (var packageFolder in packageFolders) - { - args.Add("--additionalprobingpath"); - args.Add(packageFolder.TrimEnd(Path.DirectorySeparatorChar)); - } + args.Add("--additionalprobingpath"); + args.Add(packageFolder.TrimEnd(Path.DirectorySeparatorChar)); } } diff --git a/src/ef/CommandLineUtils/CommandLineApplication.cs b/src/ef/CommandLineUtils/CommandLineApplication.cs index 49d6d045686..502b5c9766b 100644 --- a/src/ef/CommandLineUtils/CommandLineApplication.cs +++ b/src/ef/CommandLineUtils/CommandLineApplication.cs @@ -107,7 +107,6 @@ public CommandArgument Argument(string name, string description, Action arguments = null; if (HandleResponseFiles) { @@ -118,8 +117,8 @@ public int Execute(params string[] args) { var arg = args[index]; - var isLongOption = arg.StartsWith("--"); - if (isLongOption || arg.StartsWith("-")) + var isLongOption = arg.StartsWith("--", StringComparison.Ordinal); + if (isLongOption || arg.StartsWith("-", StringComparison.Ordinal)) { var result = ParseOption(isLongOption, command, args, ref index, out var option); if (result == ParseOptionResult.ShowHelp) @@ -143,10 +142,7 @@ public int Execute(params string[] args) } else { - if (arguments == null) - { - arguments = new CommandArgumentEnumerator(command.Arguments.GetEnumerator()); - } + using var arguments = new CommandArgumentEnumerator(command.Arguments.GetEnumerator()); if (arguments.MoveNext()) { diff --git a/src/ef/CommandLineUtils/CommandLineApplicationExtensions.cs b/src/ef/CommandLineUtils/CommandLineApplicationExtensions.cs index 346d8a80996..7a1ab6040cb 100644 --- a/src/ef/CommandLineUtils/CommandLineApplicationExtensions.cs +++ b/src/ef/CommandLineUtils/CommandLineApplicationExtensions.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; + namespace Microsoft.DotNet.Cli.CommandLine { internal static class CommandLineApplicationExtensions @@ -10,7 +12,7 @@ public static CommandOption Option(this CommandLineApplication command, string t template, description, template.IndexOf('<') != -1 - ? template.EndsWith(">...") + ? template.EndsWith(">...", StringComparison.Ordinal) ? CommandOptionType.MultipleValue : CommandOptionType.SingleValue : CommandOptionType.NoValue); diff --git a/src/ef/CommandLineUtils/CommandOption.cs b/src/ef/CommandLineUtils/CommandOption.cs index 16a9ea88c28..f85a9da1d14 100644 --- a/src/ef/CommandLineUtils/CommandOption.cs +++ b/src/ef/CommandLineUtils/CommandOption.cs @@ -17,11 +17,11 @@ public CommandOption(string template, CommandOptionType optionType) foreach (var part in Template.Split(new[] { ' ', '|' }, StringSplitOptions.RemoveEmptyEntries)) { - if (part.StartsWith("--")) + if (part.StartsWith("--", StringComparison.Ordinal)) { LongName = part.Substring(2); } - else if (part.StartsWith("-")) + else if (part.StartsWith("-", StringComparison.Ordinal)) { var optName = part.Substring(1); @@ -36,14 +36,14 @@ public CommandOption(string template, CommandOptionType optionType) ShortName = optName; } } - else if (part.StartsWith("<") - && part.EndsWith(">")) + else if (part.StartsWith("<", StringComparison.Ordinal) + && part.EndsWith(">", StringComparison.Ordinal)) { ValueName = part.Substring(1, part.Length - 2); } else if (optionType == CommandOptionType.MultipleValue - && part.StartsWith("<") - && part.EndsWith(">...")) + && part.StartsWith("<", StringComparison.Ordinal) + && part.EndsWith(">...", StringComparison.Ordinal)) { ValueName = part.Substring(1, part.Length - 5); } diff --git a/src/ef/Commands/DatabaseDropCommand.cs b/src/ef/Commands/DatabaseDropCommand.cs index 527ae31ef22..f5405598eb0 100644 --- a/src/ef/Commands/DatabaseDropCommand.cs +++ b/src/ef/Commands/DatabaseDropCommand.cs @@ -11,7 +11,7 @@ internal partial class DatabaseDropCommand { protected override int Execute(string[] args) { - var executor = CreateExecutor(args); + using var executor = CreateExecutor(args); void LogDropCommand(Func resource) { diff --git a/src/ef/Commands/DatabaseUpdateCommand.cs b/src/ef/Commands/DatabaseUpdateCommand.cs index 06db58b40ea..dec25755b6b 100644 --- a/src/ef/Commands/DatabaseUpdateCommand.cs +++ b/src/ef/Commands/DatabaseUpdateCommand.cs @@ -8,7 +8,9 @@ internal partial class DatabaseUpdateCommand { protected override int Execute(string[] args) { - CreateExecutor(args).UpdateDatabase(_migration.Value, _connection.Value(), Context.Value()); + using var executor = CreateExecutor(args); + + executor.UpdateDatabase(_migration.Value, _connection.Value(), Context.Value()); return base.Execute(args); } diff --git a/src/ef/Commands/DbContextInfoCommand.cs b/src/ef/Commands/DbContextInfoCommand.cs index 9fb3fd5f93d..d7a2615eb78 100644 --- a/src/ef/Commands/DbContextInfoCommand.cs +++ b/src/ef/Commands/DbContextInfoCommand.cs @@ -11,7 +11,8 @@ internal partial class DbContextInfoCommand { protected override int Execute(string[] args) { - var result = CreateExecutor(args).GetContextInfo(Context.Value()); + using var executor = CreateExecutor(args); + var result = executor.GetContextInfo(Context.Value()); if (_json.HasValue()) { diff --git a/src/ef/Commands/DbContextListCommand.cs b/src/ef/Commands/DbContextListCommand.cs index f48cc36768a..5e4ba01e8a5 100644 --- a/src/ef/Commands/DbContextListCommand.cs +++ b/src/ef/Commands/DbContextListCommand.cs @@ -13,7 +13,8 @@ internal partial class DbContextListCommand { protected override int Execute(string[] args) { - var types = CreateExecutor(args).GetContextTypes().ToList(); + using var executor = CreateExecutor(args); + var types = executor.GetContextTypes().ToList(); if (_json.HasValue()) { diff --git a/src/ef/Commands/DbContextScaffoldCommand.cs b/src/ef/Commands/DbContextScaffoldCommand.cs index c0a7202a400..67309c3fc57 100644 --- a/src/ef/Commands/DbContextScaffoldCommand.cs +++ b/src/ef/Commands/DbContextScaffoldCommand.cs @@ -27,7 +27,8 @@ protected override void Validate() protected override int Execute(string[] args) { - var result = CreateExecutor(args).ScaffoldContext( + using var executor = CreateExecutor(args); + var result = executor.ScaffoldContext( _provider.Value, _connection.Value, _outputDir.Value(), diff --git a/src/ef/Commands/DbContextScriptCommand.cs b/src/ef/Commands/DbContextScriptCommand.cs index 7c9a47ae8d6..bcde223ba67 100644 --- a/src/ef/Commands/DbContextScriptCommand.cs +++ b/src/ef/Commands/DbContextScriptCommand.cs @@ -12,7 +12,8 @@ internal partial class DbContextScriptCommand { protected override int Execute(string[] args) { - var sql = CreateExecutor(args).ScriptDbContext(Context.Value()); + using var executor = CreateExecutor(args); + var sql = executor.ScriptDbContext(Context.Value()); if (!_output.HasValue()) { diff --git a/src/ef/Commands/MigrationsAddCommand.cs b/src/ef/Commands/MigrationsAddCommand.cs index 9d15801dc9d..5ad0fc99bf9 100644 --- a/src/ef/Commands/MigrationsAddCommand.cs +++ b/src/ef/Commands/MigrationsAddCommand.cs @@ -21,8 +21,8 @@ protected override void Validate() protected override int Execute(string[] args) { - var files = CreateExecutor(args) - .AddMigration(_name.Value, _outputDir.Value(), Context.Value(), _namespace.Value()); + using var executor = CreateExecutor(args); + var files = executor.AddMigration(_name.Value, _outputDir.Value(), Context.Value(), _namespace.Value()); if (_json.HasValue()) { diff --git a/src/ef/Commands/MigrationsListCommand.cs b/src/ef/Commands/MigrationsListCommand.cs index 16de04fb4e3..c37fd196015 100644 --- a/src/ef/Commands/MigrationsListCommand.cs +++ b/src/ef/Commands/MigrationsListCommand.cs @@ -13,8 +13,8 @@ internal partial class MigrationsListCommand { protected override int Execute(string[] args) { - var migrations = CreateExecutor(args) - .GetMigrations(Context.Value(), _connection.Value(), _noConnect.HasValue()).ToList(); + using var executor = CreateExecutor(args); + var migrations = executor.GetMigrations(Context.Value(), _connection.Value(), _noConnect.HasValue()).ToList(); if (_json.HasValue()) { diff --git a/src/ef/Commands/MigrationsRemoveCommand.cs b/src/ef/Commands/MigrationsRemoveCommand.cs index 7d8cc33ec06..fd9e762af17 100644 --- a/src/ef/Commands/MigrationsRemoveCommand.cs +++ b/src/ef/Commands/MigrationsRemoveCommand.cs @@ -10,8 +10,9 @@ internal partial class MigrationsRemoveCommand { protected override int Execute(string[] args) { - var result = CreateExecutor(args) - .RemoveMigration(Context.Value(), _force.HasValue()); + using var executor = CreateExecutor(args); + var result = executor.RemoveMigration(Context.Value(), _force.HasValue()); + if (_json.HasValue()) { ReportJsonResults(result); diff --git a/src/ef/Commands/MigrationsScriptCommand.cs b/src/ef/Commands/MigrationsScriptCommand.cs index da9f922b974..0d7196fec7b 100644 --- a/src/ef/Commands/MigrationsScriptCommand.cs +++ b/src/ef/Commands/MigrationsScriptCommand.cs @@ -12,7 +12,9 @@ internal partial class MigrationsScriptCommand { protected override int Execute(string[] args) { - var sql = CreateExecutor(args).ScriptMigration( + using var executor = CreateExecutor(args); + + var sql = executor.ScriptMigration( _from.Value, _to.Value, _idempotent.HasValue(),