diff --git a/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs b/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs index 80882613505..c5684fb3fa1 100644 --- a/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs +++ b/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs @@ -128,10 +128,7 @@ protected virtual void ValidateSharedContainerCompatibility( break; } - if (firstEntityType == null) - { - firstEntityType = entityType; - } + firstEntityType ??= entityType; if (entityType.ClrType.IsInstantiable() && entityType.GetContainingPropertyName() == null) diff --git a/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs b/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs index b62d69457d2..15a2dfe99cc 100644 --- a/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs +++ b/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs @@ -34,10 +34,7 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices( IOperationReporter? reporter = null, Func? applicationServiceProviderAccessor = null) { - if (reporter == null) - { - reporter = new OperationReporter(handler: null); - } + reporter ??= new OperationReporter(handler: null); new EntityFrameworkRelationalDesignServicesBuilder(services) .TryAddProviderSpecificServices( diff --git a/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs b/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs index ac84071afec..8aa43dafd5d 100644 --- a/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs +++ b/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs @@ -352,12 +352,9 @@ public virtual MigrationFiles RemoveMigration( modelSnapshotName, model); - if (modelSnapshotFile == null) - { - modelSnapshotFile = Path.Combine( - GetDirectory(projectDir, null, GetSubNamespace(rootNamespace, modelSnapshotNamespace)), - modelSnapshotFileName); - } + modelSnapshotFile ??= Path.Combine( + GetDirectory(projectDir, null, GetSubNamespace(rootNamespace, modelSnapshotNamespace)), + modelSnapshotFileName); Dependencies.OperationReporter.WriteInformation(DesignStrings.RevertingSnapshot); File.WriteAllText(modelSnapshotFile, modelSnapshotCode, Encoding.UTF8); diff --git a/src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs b/src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs index 6d81f01cd04..ce8ec63f2e9 100644 --- a/src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs +++ b/src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs @@ -88,10 +88,7 @@ public virtual ScaffoldedModel ScaffoldModel( _reporter.WriteWarning(DesignStrings.SensitiveInformationWarning); } - if (codeOptions.ConnectionString == null) - { - codeOptions.ConnectionString = connectionString; - } + codeOptions.ConnectionString ??= connectionString; var databaseModel = _databaseModelFactory.Create(resolvedConnectionString, databaseOptions); var modelConnectionString = (string?)(databaseModel[ScaffoldingAnnotationNames.ConnectionString]); diff --git a/src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs b/src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs index 49eb277f368..76c49c00855 100644 --- a/src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs +++ b/src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs @@ -831,10 +831,7 @@ void IPrintableExpression.Print(ExpressionPrinter expressionPrinter) private InMemoryQueryExpression Clone() { - if (_cloningExpressionVisitor == null) - { - _cloningExpressionVisitor = new CloningExpressionVisitor(); - } + _cloningExpressionVisitor ??= new CloningExpressionVisitor(); return (InMemoryQueryExpression)_cloningExpressionVisitor.Visit(this); } diff --git a/src/EFCore.InMemory/Query/Internal/InMemoryShapedQueryCompilingExpressionVisitor.ShaperExpressionProcessingExpressionVisitor.cs b/src/EFCore.InMemory/Query/Internal/InMemoryShapedQueryCompilingExpressionVisitor.ShaperExpressionProcessingExpressionVisitor.cs index 2267e50a4da..72dc843bd26 100644 --- a/src/EFCore.InMemory/Query/Internal/InMemoryShapedQueryCompilingExpressionVisitor.ShaperExpressionProcessingExpressionVisitor.cs +++ b/src/EFCore.InMemory/Query/Internal/InMemoryShapedQueryCompilingExpressionVisitor.ShaperExpressionProcessingExpressionVisitor.cs @@ -57,11 +57,8 @@ public LambdaExpression ProcessShaper(Expression shaperExpression) _expressions.Add(result); result = Expression.Block(_variables, _expressions); - if (_valueBufferParameter == null) - { - // If parameter is null then the projection is not really server correlated so we can just put anything. - _valueBufferParameter = Expression.Parameter(typeof(ValueBuffer)); - } + // If parameter is null then the projection is not really server correlated so we can just put anything. + _valueBufferParameter ??= Expression.Parameter(typeof(ValueBuffer)); return Expression.Lambda(result, QueryCompilationContext.QueryContextParameter, _valueBufferParameter); } @@ -95,10 +92,7 @@ protected override Expression VisitExtension(Expression extensionExpression) variable = Expression.Parameter(projectionBindingExpression.Type); _variables.Add(variable); var queryExpression = (InMemoryQueryExpression)projectionBindingExpression.QueryExpression; - if (_valueBufferParameter == null) - { - _valueBufferParameter = queryExpression.CurrentParameter; - } + _valueBufferParameter ??= queryExpression.CurrentParameter; var projectionIndex = queryExpression.GetProjection(projectionBindingExpression).GetConstantValue(); @@ -215,10 +209,7 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression) var projectionBindingExpression = (ProjectionBindingExpression)newExpression.Arguments[0]; var queryExpression = (InMemoryQueryExpression)projectionBindingExpression.QueryExpression; - if (_valueBufferParameter == null) - { - _valueBufferParameter = queryExpression.CurrentParameter; - } + _valueBufferParameter ??= queryExpression.CurrentParameter; _materializationContextBindings[parameterExpression] = queryExpression.GetProjection(projectionBindingExpression).GetConstantValue>(); diff --git a/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs b/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs index cd6e2591951..8bd53b03a08 100644 --- a/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs +++ b/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs @@ -204,10 +204,7 @@ public virtual int ExecuteTransaction( // Must be called from inside the lock private IInMemoryTable EnsureTable(IEntityType entityType) { - if (_tables == null) - { - _tables = CreateTables(); - } + _tables ??= CreateTables(); IInMemoryTable? baseTable = null; diff --git a/src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs b/src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs index c5caabf667f..f46bd48af0d 100644 --- a/src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs +++ b/src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs @@ -148,10 +148,7 @@ public static string GetDefaultColumnName(this IReadOnlyProperty property, in St break; } - if (builder == null) - { - builder = new StringBuilder(); - } + builder ??= new StringBuilder(); builder.Insert(0, "_"); builder.Insert(0, ownership.PrincipalToDependent!.Name); diff --git a/src/EFCore.Relational/Metadata/Conventions/CheckConstraintConvention.cs b/src/EFCore.Relational/Metadata/Conventions/CheckConstraintConvention.cs index 203eb5d07ed..67d85b54857 100644 --- a/src/EFCore.Relational/Metadata/Conventions/CheckConstraintConvention.cs +++ b/src/EFCore.Relational/Metadata/Conventions/CheckConstraintConvention.cs @@ -117,10 +117,7 @@ public virtual void ProcessEntityTypeBaseTypeChanged( baseCheckConstraint.EntityType.DisplayName())); } - if (checkConstraintsToBeRemoved == null) - { - checkConstraintsToBeRemoved = new List(); - } + checkConstraintsToBeRemoved ??= new List(); checkConstraintsToBeRemoved.Add(checkConstraint); continue; @@ -128,10 +125,7 @@ public virtual void ProcessEntityTypeBaseTypeChanged( if (baseCheckConstraint != null) { - if (checkConstraintsToBeDetached == null) - { - checkConstraintsToBeDetached = new List(); - } + checkConstraintsToBeDetached ??= new List(); checkConstraintsToBeDetached.Add(checkConstraint); } diff --git a/src/EFCore.Relational/Metadata/Conventions/TableNameFromDbSetConvention.cs b/src/EFCore.Relational/Metadata/Conventions/TableNameFromDbSetConvention.cs index 411e581c292..ef4751b1d68 100644 --- a/src/EFCore.Relational/Metadata/Conventions/TableNameFromDbSetConvention.cs +++ b/src/EFCore.Relational/Metadata/Conventions/TableNameFromDbSetConvention.cs @@ -32,10 +32,7 @@ public TableNameFromDbSetConvention( } else { - if (ambiguousTypes == null) - { - ambiguousTypes = new List(); - } + ambiguousTypes ??= new List(); ambiguousTypes.Add(set.Type); } diff --git a/src/EFCore.Relational/Metadata/Conventions/TableSharingConcurrencyTokenConvention.cs b/src/EFCore.Relational/Metadata/Conventions/TableSharingConcurrencyTokenConvention.cs index 39e8ecc9698..4e823610b9d 100644 --- a/src/EFCore.Relational/Metadata/Conventions/TableSharingConcurrencyTokenConvention.cs +++ b/src/EFCore.Relational/Metadata/Conventions/TableSharingConcurrencyTokenConvention.cs @@ -89,10 +89,7 @@ public virtual void ProcessModelFinalizing( if (!foundMappedProperty) { - if (entityTypesMissingConcurrencyColumn == null) - { - entityTypesMissingConcurrencyColumn = new Dictionary(); - } + entityTypesMissingConcurrencyColumn ??= new Dictionary(); // store the entity type which is missing the // concurrency token property, mapped to an example @@ -166,10 +163,7 @@ public virtual void ProcessModelFinalizing( continue; } - if (concurrencyColumns == null) - { - concurrencyColumns = new Dictionary>(); - } + concurrencyColumns ??= new Dictionary>(); if (!concurrencyColumns.TryGetValue(columnName, out var properties)) { diff --git a/src/EFCore.Relational/Metadata/Internal/InternalCheckConstraintBuilder.cs b/src/EFCore.Relational/Metadata/Internal/InternalCheckConstraintBuilder.cs index 1415ffe4fb4..727c1147f29 100644 --- a/src/EFCore.Relational/Metadata/Internal/InternalCheckConstraintBuilder.cs +++ b/src/EFCore.Relational/Metadata/Internal/InternalCheckConstraintBuilder.cs @@ -98,10 +98,7 @@ public virtual bool CanSetName(string? name, ConfigurationSource configurationSo return null; } - if (checkConstraintsToBeDetached == null) - { - checkConstraintsToBeDetached = new List(); - } + checkConstraintsToBeDetached ??= new List(); checkConstraintsToBeDetached.Add(derivedCheckConstraint); } diff --git a/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs b/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs index 8e07457807c..05071f2467d 100644 --- a/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs +++ b/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs @@ -1001,18 +1001,12 @@ private static void PopulateRowInternalForeignKeys(TableBase table) && !foreignKey.PrincipalEntityType.IsAssignableFrom(foreignKey.DeclaringEntityType) && ((ITableBase)table).EntityTypeMappings.Any(m => m.EntityType == foreignKey.PrincipalEntityType)) { - if (rowInternalForeignKeys == null) - { - rowInternalForeignKeys = new SortedSet(ForeignKeyComparer.Instance); - } + rowInternalForeignKeys ??= new SortedSet(ForeignKeyComparer.Instance); rowInternalForeignKeys.Add(foreignKey); - if (referencingInternalForeignKeyMap == null) - { - referencingInternalForeignKeyMap = - new SortedDictionary>(EntityTypeFullNameComparer.Instance); - } + referencingInternalForeignKeyMap ??= + new SortedDictionary>(EntityTypeFullNameComparer.Instance); var principalEntityType = foreignKey.PrincipalEntityType; if (!referencingInternalForeignKeyMap.TryGetValue(principalEntityType, out var internalReferencingForeignKeys)) diff --git a/src/EFCore.Relational/Migrations/MigrationBuilder.cs b/src/EFCore.Relational/Migrations/MigrationBuilder.cs index 1be57676378..768e45ce2af 100644 --- a/src/EFCore.Relational/Migrations/MigrationBuilder.cs +++ b/src/EFCore.Relational/Migrations/MigrationBuilder.cs @@ -837,10 +837,7 @@ public virtual CreateTableBuilder CreateTable( foreach (var property in typeof(TColumns).GetTypeInfo().DeclaredProperties) { var addColumnOperation = ((IInfrastructure)property.GetMethod!.Invoke(columnsObject, null)!).Instance; - if (addColumnOperation.Name == null) - { - addColumnOperation.Name = property.Name; - } + addColumnOperation.Name ??= property.Name; // TODO: addColumnOperation.Validate(); columnMap.Add(property, addColumnOperation); diff --git a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.Helper.cs b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.Helper.cs index 1ec022f86eb..1f6fda7fb22 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.Helper.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.Helper.cs @@ -241,10 +241,7 @@ private sealed class ColumnExpressionFindingExpressionVisitor : ExpressionVisito var tableAlias = columnExpression.TableAlias!; if (_columnReferenced!.ContainsKey(tableAlias)) { - if (_columnReferenced[tableAlias] == null) - { - _columnReferenced[tableAlias] = new HashSet(); - } + _columnReferenced[tableAlias] ??= new HashSet(); _columnReferenced[tableAlias]!.Add(columnExpression.Name); } diff --git a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs index f6d604e713d..693254cbc12 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs @@ -2855,10 +2855,7 @@ public void PrepareForAggregate() [EntityFrameworkInternal] public SelectExpression Clone() { - if (_cloningExpressionVisitor == null) - { - _cloningExpressionVisitor = new CloningExpressionVisitor(); - } + _cloningExpressionVisitor ??= new CloningExpressionVisitor(); return (SelectExpression)_cloningExpressionVisitor.Visit(this); } diff --git a/src/EFCore.Relational/Storage/RelationalCommand.cs b/src/EFCore.Relational/Storage/RelationalCommand.cs index 08a6cce24b2..5e6dad652e6 100644 --- a/src/EFCore.Relational/Storage/RelationalCommand.cs +++ b/src/EFCore.Relational/Storage/RelationalCommand.cs @@ -508,10 +508,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje reader = new BufferedDataReader(reader, detailedErrorsEnabled).Initialize(readerColumns); } - if (_relationalReader == null) - { - _relationalReader = CreateRelationalDataReader(); - } + _relationalReader ??= CreateRelationalDataReader(); _relationalReader.Initialize(parameterObject.Connection, command, reader, commandId, logger); @@ -633,10 +630,7 @@ await logger.CommandErrorAsync( .ConfigureAwait(false); } - if (_relationalReader == null) - { - _relationalReader = CreateRelationalDataReader(); - } + _relationalReader ??= CreateRelationalDataReader(); _relationalReader.Initialize(parameterObject.Connection, command, reader, commandId, logger); diff --git a/src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs b/src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs index 7e701215b76..525b4fee2ce 100644 --- a/src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs +++ b/src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs @@ -212,10 +212,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo) } } - if (isFixedLength == null) - { - isFixedLength = principal.IsFixedLength(); - } + isFixedLength ??= principal.IsFixedLength(); } var storeTypeNameBase = ParseStoreTypeName(storeTypeName, out var unicode, out var size, out var precision, out var scale); @@ -281,25 +278,10 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo) storeTypeBaseName = ParseStoreTypeName( storeTypeName, out var parsedUnicode, out var parsedSize, out var parsedPrecision, out var parsedScale); - if (size == null) - { - size = parsedSize; - } - - if (precision == null) - { - precision = parsedPrecision; - } - - if (scale == null) - { - scale = parsedScale; - } - - if (isUnicode == null) - { - isUnicode = parsedUnicode; - } + size ??= parsedSize; + precision ??= parsedPrecision; + scale ??= parsedScale; + isUnicode ??= parsedUnicode; } var isFixedLength = (bool?)typeConfiguration[RelationalAnnotationNames.IsFixedLength]; @@ -411,25 +393,11 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo) { storeTypeBaseName = ParseStoreTypeName( storeTypeName, out var parsedUnicode, out var parsedSize, out var parsedPrecision, out var parsedScale); - if (size == null) - { - size = parsedSize; - } - - if (precision == null) - { - precision = parsedPrecision; - } - if (scale == null) - { - scale = parsedScale; - } - - if (unicode == null) - { - unicode = parsedUnicode; - } + size ??= parsedSize; + precision ??= parsedPrecision; + scale ??= parsedScale; + unicode ??= parsedUnicode; } return FindMappingWithConversion( diff --git a/src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs b/src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs index 2a81dae8fb5..85885d97c2d 100644 --- a/src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs +++ b/src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs @@ -167,10 +167,7 @@ protected virtual IEnumerable CreateModificationCo var isMainEntry = true; if (table.IsShared) { - if (sharedTablesCommandsMap == null) - { - sharedTablesCommandsMap = new Dictionary<(string, string?), SharedTableEntryMap>(); - } + sharedTablesCommandsMap ??= new Dictionary<(string, string?), SharedTableEntryMap>(); var tableKey = (table.Name, table.Schema); if (!sharedTablesCommandsMap.TryGetValue(tableKey, out var sharedCommandsMap)) diff --git a/src/EFCore.Relational/Update/ModificationCommand.cs b/src/EFCore.Relational/Update/ModificationCommand.cs index 95ac32bd3bf..fbfffda249a 100644 --- a/src/EFCore.Relational/Update/ModificationCommand.cs +++ b/src/EFCore.Relational/Update/ModificationCommand.cs @@ -213,10 +213,7 @@ public virtual IColumnModification AddColumnModification(in ColumnModificationPa { var modification = CreateColumnModification(columnModificationParameters); - if (_columnModifications == null) - { - _columnModifications = new List(); - } + _columnModifications ??= new List(); _columnModifications.Add(modification); diff --git a/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs b/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs index b4f0d5f3f5f..448e7302e2b 100644 --- a/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs +++ b/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs @@ -1696,10 +1696,7 @@ public virtual bool CanSetDefiningQuery(LambdaExpression? query, ConfigurationSo { if (Metadata.IsAssignableFrom(referencingForeignKey.PrincipalEntityType)) { - if (relationshipsToBeDetached == null) - { - relationshipsToBeDetached = new List(); - } + relationshipsToBeDetached ??= new List(); relationshipsToBeDetached.Add(referencingForeignKey); } @@ -1733,10 +1730,7 @@ public virtual bool CanSetDefiningQuery(LambdaExpression? query, ConfigurationSo continue; } - if (indexesToBeDetached == null) - { - indexesToBeDetached = new List(); - } + indexesToBeDetached ??= new List(); indexesToBeDetached.Add(index); } @@ -1848,10 +1842,7 @@ public virtual bool CanSetDefiningQuery(LambdaExpression? query, ConfigurationSo member.Name)); } - if (membersToBeRemoved == null) - { - membersToBeRemoved = new List(); - } + membersToBeRemoved ??= new List(); membersToBeRemoved.Add(member); continue; @@ -2174,10 +2165,7 @@ public static RelationshipSnapshot DetachRelationship(ForeignKey foreignKey, boo continue; } - if (detachedRelationships == null) - { - detachedRelationships = new List(); - } + detachedRelationships ??= new List(); var detachedRelationship = DetachRelationship(relationshipToBeDetached, true); if (detachedRelationship.Relationship.Metadata.GetConfigurationSource().Overrides(ConfigurationSource.DataAnnotation) @@ -3734,10 +3722,7 @@ private static bool Contains(IReadOnlyForeignKey? inheritedFk, IReadOnlyForeignK } } - if (targetType == null) - { - targetType = Model.DefaultPropertyBagType; - } + targetType ??= Model.DefaultPropertyBagType; switch (ModelBuilder.Metadata.Configuration?.GetConfigurationType(targetType)) { @@ -4179,10 +4164,7 @@ private static bool Contains(IReadOnlyForeignKey? inheritedFk, IReadOnlyForeignK var conflictingNavigation = derivedType.FindDeclaredSkipNavigation(navigationName); if (conflictingNavigation != null) { - if (navigationsToDetach == null) - { - navigationsToDetach = new List(); - } + navigationsToDetach ??= new List(); navigationsToDetach.Add(conflictingNavigation); } diff --git a/src/dotnet-ef/Project.cs b/src/dotnet-ef/Project.cs index cd4c4fefe57..b876400f3f0 100644 --- a/src/dotnet-ef/Project.cs +++ b/src/dotnet-ef/Project.cs @@ -49,10 +49,7 @@ public static Project FromFile( { Debug.Assert(!string.IsNullOrEmpty(file), "file is null or empty."); - if (buildExtensionsDir == null) - { - buildExtensionsDir = Path.Combine(Path.GetDirectoryName(file)!, "obj"); - } + buildExtensionsDir ??= Path.Combine(Path.GetDirectoryName(file)!, "obj"); Directory.CreateDirectory(buildExtensionsDir);