From 71f418c94b5533729b274a658ee5c246588ed8ca Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Fri, 30 Oct 2020 09:39:29 +0200 Subject: [PATCH] Nullable annotations for metadata 2 Annotates Model, Property, Navigation, Key, Index, and all their extensions. Part of #19007 --- .../ChangeTracking/Internal/KeyPropagator.cs | 2 +- .../Internal/KeyValueFactoryFactory.cs | 1 - .../ChangeTracking/ValueComparerExtensions.cs | 2 + .../ConventionEntityTypeExtensions.cs | 75 ++++++----- .../ConventionNavigationExtensions.cs | 2 + .../ConventionPropertyBaseExtensions.cs | 2 + .../ConventionPropertyExtensions.cs | 28 ++-- .../ConventionTypeBaseExtensions.cs | 2 + src/EFCore/Extensions/EntityTypeExtensions.cs | 57 ++++---- src/EFCore/Extensions/ForeignKeyExtensions.cs | 10 +- src/EFCore/Extensions/IndexExtensions.cs | 2 + src/EFCore/Extensions/KeyExtensions.cs | 2 + src/EFCore/Extensions/ModelExtensions.cs | 10 +- .../MutableAnnotatableExtensions.cs | 4 +- .../Extensions/MutableEntityTypeExtensions.cs | 60 +++++---- .../Extensions/MutableForeignKeyExtensions.cs | 4 +- src/EFCore/Extensions/MutableKeyExtensions.cs | 2 + .../Extensions/MutableModelExtensions.cs | 26 ++-- .../Extensions/MutableNavigationExtensions.cs | 2 + .../MutablePropertyBaseExtensions.cs | 2 + .../Extensions/MutablePropertyExtensions.cs | 20 +-- .../Extensions/MutableTypeBaseExtensions.cs | 2 + src/EFCore/Extensions/NavigationExtensions.cs | 2 + .../Extensions/PropertyBaseExtensions.cs | 4 +- src/EFCore/Extensions/PropertyExtensions.cs | 33 ++--- .../Extensions/ServicePropertyExtensions.cs | 2 + .../Extensions/SkipNavigationExtensions.cs | 2 + src/EFCore/Extensions/TypeBaseExtensions.cs | 2 + .../Infrastructure/AnnotatableExtensions.cs | 2 + .../Infrastructure/ConventionAnnotatable.cs | 44 +++--- .../Builders/IConventionPropertyBuilder.cs | 6 +- src/EFCore/Metadata/IConventionAnnotatable.cs | 2 +- src/EFCore/Metadata/IConventionEntityType.cs | 10 +- src/EFCore/Metadata/IConventionForeignKey.cs | 24 ++-- src/EFCore/Metadata/IConventionIndex.cs | 4 +- src/EFCore/Metadata/IConventionKey.cs | 4 +- src/EFCore/Metadata/IConventionModel.cs | 26 ++-- src/EFCore/Metadata/IConventionNavigation.cs | 10 +- .../Metadata/IConventionNavigationBase.cs | 2 + src/EFCore/Metadata/IConventionProperty.cs | 4 +- .../Metadata/IConventionSkipNavigation.cs | 20 +-- src/EFCore/Metadata/IForeignKey.cs | 6 +- src/EFCore/Metadata/IIndex.cs | 4 +- src/EFCore/Metadata/IKey.cs | 2 + src/EFCore/Metadata/IModel.cs | 6 +- src/EFCore/Metadata/IMutableEntityType.cs | 24 ++-- src/EFCore/Metadata/IMutableForeignKey.cs | 22 +-- src/EFCore/Metadata/IMutableIndex.cs | 2 + src/EFCore/Metadata/IMutableKey.cs | 2 + src/EFCore/Metadata/IMutableModel.cs | 12 +- src/EFCore/Metadata/IMutableNavigation.cs | 6 +- src/EFCore/Metadata/IMutableNavigationBase.cs | 2 + src/EFCore/Metadata/IMutablePropertyBase.cs | 6 +- .../Metadata/IMutableServiceProperty.cs | 4 +- src/EFCore/Metadata/IMutableSkipNavigation.cs | 18 +-- src/EFCore/Metadata/IMutableTypeBase.cs | 6 +- src/EFCore/Metadata/IPropertyBase.cs | 6 +- src/EFCore/Metadata/ISkipNavigation.cs | 4 +- .../ConventionAnnotatableExtensions.cs | 6 +- .../Metadata/Internal/ConventionAnnotation.cs | 4 +- .../Metadata/Internal/CoreAnnotationNames.cs | 2 + src/EFCore/Metadata/Internal/EntityType.cs | 49 ++++--- .../Metadata/Internal/EntityTypeExtensions.cs | 30 +++-- src/EFCore/Metadata/Internal/ForeignKey.cs | 104 +++++++------- .../Metadata/Internal/ForeignKeyExtensions.cs | 4 +- src/EFCore/Metadata/Internal/Index.cs | 18 +-- .../Metadata/Internal/IndexExtensions.cs | 2 + .../Internal/InternalPropertyBuilder.cs | 2 +- src/EFCore/Metadata/Internal/Key.cs | 20 +-- src/EFCore/Metadata/Internal/KeyExtensions.cs | 2 + src/EFCore/Metadata/Internal/Model.cs | 127 ++++++++++-------- .../Metadata/Internal/ModelExtensions.cs | 2 + .../Internal/MutableEntityTypeExtensions.cs | 2 + src/EFCore/Metadata/Internal/Navigation.cs | 47 ++++--- .../Metadata/Internal/NavigationExtensions.cs | 4 +- src/EFCore/Metadata/Internal/Property.cs | 60 +++++---- src/EFCore/Metadata/Internal/PropertyBase.cs | 44 +++--- .../Metadata/Internal/PropertyExtensions.cs | 6 +- .../Metadata/Internal/ServiceProperty.cs | 24 ++-- .../Internal/ServicePropertyExtensions.cs | 4 +- .../Metadata/Internal/SkipNavigation.cs | 65 ++++----- .../Internal/SkipNavigationComparer.cs | 2 + .../Internal/SkipNavigationExtensions.cs | 4 +- src/EFCore/Properties/CoreStrings.Designer.cs | 8 ++ src/EFCore/Properties/CoreStrings.resx | 3 + src/EFCore/Query/EntityShaperExpression.cs | 2 +- .../Internal/EntityMaterializerSource.cs | 8 +- .../NavigationExpandingExpressionVisitor.cs | 7 +- src/Shared/Check.cs | 29 ++-- .../Extensions/PropertyExtensionsTest.cs | 22 +-- 90 files changed, 779 insertions(+), 586 deletions(-) diff --git a/src/EFCore/ChangeTracking/Internal/KeyPropagator.cs b/src/EFCore/ChangeTracking/Internal/KeyPropagator.cs index 542c37e1299..7fda46dfff3 100644 --- a/src/EFCore/ChangeTracking/Internal/KeyPropagator.cs +++ b/src/EFCore/ChangeTracking/Internal/KeyPropagator.cs @@ -184,7 +184,7 @@ private static InternalEntityEntry TryPropagateValue(InternalEntityEntry entry, private ValueGenerator TryGetValueGenerator(IProperty property) { - var generationProperty = property.GetGenerationProperty(); + var generationProperty = property.FindGenerationProperty(); return generationProperty != null ? _valueGeneratorSelector.Select(generationProperty, generationProperty.DeclaringEntityType) diff --git a/src/EFCore/ChangeTracking/Internal/KeyValueFactoryFactory.cs b/src/EFCore/ChangeTracking/Internal/KeyValueFactoryFactory.cs index 7ed4f7f32e5..e5c694c97c9 100644 --- a/src/EFCore/ChangeTracking/Internal/KeyValueFactoryFactory.cs +++ b/src/EFCore/ChangeTracking/Internal/KeyValueFactoryFactory.cs @@ -28,7 +28,6 @@ public virtual IPrincipalKeyValueFactory Create([NotNull] IKey key) ? CreateSimpleFactory(key) : (IPrincipalKeyValueFactory)CreateCompositeFactory(key); - [UsedImplicitly] private static SimplePrincipalKeyValueFactory CreateSimpleFactory(IKey key) { var dependentFactory = new DependentKeyValueFactoryFactory(); diff --git a/src/EFCore/ChangeTracking/ValueComparerExtensions.cs b/src/EFCore/ChangeTracking/ValueComparerExtensions.cs index 3d96ad50c22..616fd371e86 100644 --- a/src/EFCore/ChangeTracking/ValueComparerExtensions.cs +++ b/src/EFCore/ChangeTracking/ValueComparerExtensions.cs @@ -3,6 +3,8 @@ using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.ChangeTracking { /// diff --git a/src/EFCore/Extensions/ConventionEntityTypeExtensions.cs b/src/EFCore/Extensions/ConventionEntityTypeExtensions.cs index 6c391e9fec4..3ac62ffd88f 100644 --- a/src/EFCore/Extensions/ConventionEntityTypeExtensions.cs +++ b/src/EFCore/Extensions/ConventionEntityTypeExtensions.cs @@ -14,6 +14,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -93,10 +95,11 @@ public static IEnumerable GetAllBaseTypesInclusiveAscendi { Check.NotNull(entityType, nameof(entityType)); - while (entityType != null) + var tmp = (IConventionEntityType?)entityType; + while (tmp != null) { - yield return entityType; - entityType = entityType.BaseType; + yield return tmp; + tmp = tmp.BaseType; } } @@ -181,7 +184,7 @@ public static IEnumerable GetDeclaredIndexes([NotNull] this IC /// The entity type. /// The name of the property to remove. /// The property that was removed. - public static IConventionProperty RemoveProperty([NotNull] this IConventionEntityType entityType, [NotNull] string name) + public static IConventionProperty? RemoveProperty([NotNull] this IConventionEntityType entityType, [NotNull] string name) => ((EntityType)entityType).RemoveProperty(name); /// @@ -191,7 +194,7 @@ public static IConventionProperty RemoveProperty([NotNull] this IConventionEntit /// The entity type. /// The property that the key is defined on. /// The key, or null if none is defined. - public static IConventionKey FindKey([NotNull] this IConventionEntityType entityType, [NotNull] IProperty property) + public static IConventionKey? FindKey([NotNull] this IConventionEntityType entityType, [NotNull] IProperty property) { Check.NotNull(entityType, nameof(entityType)); @@ -205,7 +208,7 @@ public static IConventionKey FindKey([NotNull] this IConventionEntityType entity /// The property to use as an alternate key. /// Indicates whether the configuration was specified using a data annotation. /// The newly created key. - public static IConventionKey AddKey( + public static IConventionKey? AddKey( [NotNull] this IConventionEntityType entityType, [NotNull] IConventionProperty property, bool fromDataAnnotation = false) @@ -217,7 +220,7 @@ public static IConventionKey AddKey( /// The entity type. /// The properties that make up the key. /// The key that was removed. - public static IConventionKey RemoveKey( + public static IConventionKey? RemoveKey( [NotNull] this IConventionEntityType entityType, [NotNull] IReadOnlyList properties) => ((EntityType)entityType).RemoveKey(properties); @@ -289,7 +292,7 @@ public static IEnumerable FindForeignKeys( /// base type of the hierarchy). /// /// The foreign key, or if none is defined. - public static IConventionForeignKey FindForeignKey( + public static IConventionForeignKey? FindForeignKey( [NotNull] this IConventionEntityType entityType, [NotNull] IProperty property, [NotNull] IKey principalKey, @@ -334,7 +337,7 @@ public static IEnumerable GetDeclaredReferencingForeignKe /// /// The entity type. /// The relationship to the owner if this is an owned type or otherwise. - public static IConventionForeignKey FindOwnership([NotNull] this IConventionEntityType entityType) + public static IConventionForeignKey? FindOwnership([NotNull] this IConventionEntityType entityType) => ((EntityType)entityType).FindOwnership(); /// @@ -350,7 +353,7 @@ public static IConventionForeignKey FindOwnership([NotNull] this IConventionEnti /// /// Indicates whether the configuration was specified using a data annotation. /// The newly created foreign key. - public static IConventionForeignKey AddForeignKey( + public static IConventionForeignKey? AddForeignKey( [NotNull] this IConventionEntityType entityType, [NotNull] IConventionProperty property, [NotNull] IConventionKey principalKey, @@ -371,7 +374,7 @@ public static IConventionForeignKey AddForeignKey( /// base type of the hierarchy). /// /// The foreign key that was removed. - public static IConventionForeignKey RemoveForeignKey( + public static IConventionForeignKey? RemoveForeignKey( [NotNull] this IConventionEntityType entityType, [NotNull] IReadOnlyList properties, [NotNull] IConventionKey principalKey, @@ -384,7 +387,7 @@ public static IConventionForeignKey RemoveForeignKey( /// The entity type. /// The navigation property on the entity class. /// The navigation property, or if none is found. - public static IConventionNavigation FindNavigation( + public static IConventionNavigation? FindNavigation( [NotNull] this IConventionEntityType entityType, [NotNull] MemberInfo memberInfo) => Check.NotNull(entityType, nameof(entityType)) @@ -396,7 +399,7 @@ public static IConventionNavigation FindNavigation( /// The entity type. /// The name of the navigation property on the entity class. /// The navigation property, or if none is found. - public static IConventionNavigation FindNavigation([NotNull] this IConventionEntityType entityType, [NotNull] string name) + public static IConventionNavigation? FindNavigation([NotNull] this IConventionEntityType entityType, [NotNull] string name) => ((EntityType)entityType).FindNavigation(name); /// @@ -406,7 +409,7 @@ public static IConventionNavigation FindNavigation([NotNull] this IConventionEnt /// The entity type. /// The name of the navigation property on the entity class. /// The navigation property, or if none is found. - public static IConventionNavigation FindDeclaredNavigation([NotNull] this IConventionEntityType entityType, [NotNull] string name) + public static IConventionNavigation? FindDeclaredNavigation([NotNull] this IConventionEntityType entityType, [NotNull] string name) => ((EntityType)entityType).FindDeclaredNavigation(Check.NotNull(name, nameof(name))); /// @@ -414,8 +417,8 @@ public static IConventionNavigation FindDeclaredNavigation([NotNull] this IConve /// /// The entity type. /// The defining navigation if one exists or otherwise. - public static IConventionNavigation FindDefiningNavigation([NotNull] this IConventionEntityType entityType) - => (IConventionNavigation)((IEntityType)entityType).FindDefiningNavigation(); + public static IConventionNavigation? FindDefiningNavigation([NotNull] this IConventionEntityType entityType) + => (IConventionNavigation?)((IEntityType)entityType).FindDefiningNavigation(); /// /// Gets all navigation properties on the given entity type. @@ -437,7 +440,7 @@ public static IEnumerable GetNavigations([NotNull] this I /// The entity type. /// The property on the entity class. /// The property, or if none is found. - public static IConventionProperty FindProperty([NotNull] this IConventionEntityType entityType, [NotNull] MemberInfo memberInfo) + public static IConventionProperty? FindProperty([NotNull] this IConventionEntityType entityType, [NotNull] MemberInfo memberInfo) { Check.NotNull(entityType, nameof(entityType)); Check.NotNull(memberInfo, nameof(memberInfo)); @@ -458,7 +461,7 @@ public static IConventionProperty FindProperty([NotNull] this IConventionEntityT /// The entity type. /// The property names. /// The properties, or if any property is not found. - public static IReadOnlyList FindProperties( + public static IReadOnlyList? FindProperties( [NotNull] this IConventionEntityType entityType, [NotNull] IReadOnlyList propertyNames) => ((EntityType)entityType).FindProperties(Check.NotNull(propertyNames, nameof(propertyNames))); @@ -470,7 +473,7 @@ public static IReadOnlyList FindProperties( /// The entity type. /// The property name. /// The property, or if none is found. - public static IConventionProperty FindDeclaredProperty([NotNull] this IConventionEntityType entityType, [NotNull] string name) + public static IConventionProperty? FindDeclaredProperty([NotNull] this IConventionEntityType entityType, [NotNull] string name) => ((EntityType)entityType).FindDeclaredProperty(name); /// @@ -480,7 +483,7 @@ public static IConventionProperty FindDeclaredProperty([NotNull] this IConventio /// The corresponding member on the entity class. /// Indicates whether the configuration was specified using a data annotation. /// The newly created property. - public static IConventionProperty AddProperty( + public static IConventionProperty? AddProperty( [NotNull] this IConventionEntityType entityType, [NotNull] MemberInfo memberInfo, bool fromDataAnnotation = false) @@ -495,7 +498,7 @@ public static IConventionProperty AddProperty( /// The name of the property to add. /// Indicates whether the configuration was specified using a data annotation. /// The newly created property. - public static IConventionProperty AddProperty( + public static IConventionProperty? AddProperty( [NotNull] this IConventionEntityType entityType, [NotNull] string name, bool fromDataAnnotation = false) @@ -511,7 +514,7 @@ public static IConventionProperty AddProperty( /// Indicates whether the type configuration source should be set. /// Indicates whether the configuration was specified using a data annotation. /// The newly created property. - public static IConventionProperty AddProperty( + public static IConventionProperty? AddProperty( [NotNull] this IConventionEntityType entityType, [NotNull] string name, [NotNull] Type propertyType, @@ -534,7 +537,7 @@ public static IConventionProperty AddProperty( /// Indicates whether the type configuration source should be set. /// Indicates whether the configuration was specified using a data annotation. /// The newly created property. - public static IConventionProperty AddIndexerProperty( + public static IConventionProperty? AddIndexerProperty( [NotNull] this IConventionEntityType entityType, [NotNull] string name, [NotNull] Type propertyType, @@ -564,7 +567,7 @@ public static IConventionProperty AddIndexerProperty( /// The entity type. /// The property to find the index on. /// The index, or null if none is found. - public static IConventionIndex FindIndex([NotNull] this IConventionEntityType entityType, [NotNull] IProperty property) + public static IConventionIndex? FindIndex([NotNull] this IConventionEntityType entityType, [NotNull] IProperty property) { Check.NotNull(entityType, nameof(entityType)); @@ -578,7 +581,7 @@ public static IConventionIndex FindIndex([NotNull] this IConventionEntityType en /// The property to be indexed. /// Indicates whether the configuration was specified using a data annotation. /// The newly created index. - public static IConventionIndex AddIndex( + public static IConventionIndex? AddIndex( [NotNull] this IConventionEntityType entityType, [NotNull] IConventionProperty property, bool fromDataAnnotation = false) @@ -590,7 +593,7 @@ public static IConventionIndex AddIndex( /// The entity type. /// The properties that make up the index. /// The index that was removed. - public static IConventionIndex RemoveIndex( + public static IConventionIndex? RemoveIndex( [NotNull] this IConventionEntityType entityType, [NotNull] IReadOnlyList properties) => ((EntityType)entityType).RemoveIndex(properties); @@ -627,9 +630,9 @@ public static IConventionIndex RemoveIndex( /// The LINQ expression filter. /// Indicates whether the configuration was specified using a data annotation. /// The configured filter. - public static LambdaExpression SetQueryFilter( + public static LambdaExpression? SetQueryFilter( [NotNull] this IConventionEntityType entityType, - [CanBeNull] LambdaExpression queryFilter, + [CanBeNull] LambdaExpression? queryFilter, bool fromDataAnnotation = false) => Check.NotNull(entityType, nameof(entityType)).AsEntityType() .SetQueryFilter( @@ -653,7 +656,7 @@ public static LambdaExpression SetQueryFilter( [Obsolete("Use InMemoryEntityTypeExtensions.SetInMemoryQuery")] public static void SetDefiningQuery( [NotNull] this IConventionEntityType entityType, - [CanBeNull] LambdaExpression definingQuery, + [CanBeNull] LambdaExpression? definingQuery, bool fromDataAnnotation = false) => Check.NotNull(entityType, nameof(entityType)).AsEntityType() .SetDefiningQuery( @@ -673,8 +676,8 @@ public static void SetDefiningQuery( /// Returns the that will be used for storing a discriminator value. /// /// The entity type. - public static IConventionProperty GetDiscriminatorProperty([NotNull] this IConventionEntityType entityType) - => (IConventionProperty)((IEntityType)entityType).GetDiscriminatorProperty(); + public static IConventionProperty? GetDiscriminatorProperty([NotNull] this IConventionEntityType entityType) + => (IConventionProperty?)((IEntityType)entityType).GetDiscriminatorProperty(); /// /// Sets the that will be used for storing a discriminator value. @@ -683,13 +686,13 @@ public static IConventionProperty GetDiscriminatorProperty([NotNull] this IConve /// The property to set. /// Indicates whether the configuration was specified using a data annotation. /// The discriminator property. - public static IConventionProperty SetDiscriminatorProperty( + public static IConventionProperty? SetDiscriminatorProperty( [NotNull] this IConventionEntityType entityType, - [CanBeNull] IProperty property, + [CanBeNull] IProperty? property, bool fromDataAnnotation = false) => Check.NotNull(entityType, nameof(entityType)).AsEntityType() .SetDiscriminatorProperty( - (Property)property, + (Property?)property, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// @@ -739,7 +742,7 @@ public static IConventionProperty SetDiscriminatorProperty( /// The configured value. public static object SetDiscriminatorValue( [NotNull] this IConventionEntityType entityType, - [CanBeNull] object value, + [CanBeNull] object? value, bool fromDataAnnotation = false) { entityType.AsEntityType().CheckDiscriminatorValue(entityType, value); @@ -754,7 +757,7 @@ public static object SetDiscriminatorValue( /// /// The entity type. /// The removed discriminator value. - public static object RemoveDiscriminatorValue([NotNull] this IConventionEntityType entityType) + public static object? RemoveDiscriminatorValue([NotNull] this IConventionEntityType entityType) => entityType.RemoveAnnotation(CoreAnnotationNames.DiscriminatorValue)?.Value; /// diff --git a/src/EFCore/Extensions/ConventionNavigationExtensions.cs b/src/EFCore/Extensions/ConventionNavigationExtensions.cs index 1493357977d..c02851d4da9 100644 --- a/src/EFCore/Extensions/ConventionNavigationExtensions.cs +++ b/src/EFCore/Extensions/ConventionNavigationExtensions.cs @@ -5,6 +5,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/ConventionPropertyBaseExtensions.cs b/src/EFCore/Extensions/ConventionPropertyBaseExtensions.cs index 71a26bed22b..6f17a629af2 100644 --- a/src/EFCore/Extensions/ConventionPropertyBaseExtensions.cs +++ b/src/EFCore/Extensions/ConventionPropertyBaseExtensions.cs @@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/ConventionPropertyExtensions.cs b/src/EFCore/Extensions/ConventionPropertyExtensions.cs index b551859f6a6..b605bbea169 100644 --- a/src/EFCore/Extensions/ConventionPropertyExtensions.cs +++ b/src/EFCore/Extensions/ConventionPropertyExtensions.cs @@ -12,6 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.ValueGeneration; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -26,8 +28,8 @@ public static class ConventionPropertyExtensions /// /// The foreign key property. /// The first associated principal property, or if none exists. - public static IConventionProperty FindFirstPrincipal([NotNull] this IConventionProperty property) - => (IConventionProperty)((IProperty)property).FindFirstPrincipal(); + public static IConventionProperty? FindFirstPrincipal([NotNull] this IConventionProperty property) + => (IConventionProperty?)((IProperty)property).FindFirstPrincipal(); /// /// Finds the list of principal properties including the given property that the given property is constrained by @@ -68,8 +70,8 @@ public static IEnumerable GetContainingIndexes([NotNull] this /// /// The primary that use this property, or if it is not part of the primary key. /// - public static IConventionKey FindContainingPrimaryKey([NotNull] this IConventionProperty property) - => (IConventionKey)((IProperty)property).FindContainingPrimaryKey(); + public static IConventionKey? FindContainingPrimaryKey([NotNull] this IConventionProperty property) + => (IConventionKey?)((IProperty)property).FindContainingPrimaryKey(); /// /// Gets all primary or alternate keys that use this property (including composite keys in which this property @@ -280,7 +282,7 @@ public static CoreTypeMapping SetTypeMapping( /// /// Indicates whether the configuration was specified using a data annotation. /// The configured value. - public static Func SetValueGeneratorFactory( + public static Func? SetValueGeneratorFactory( [NotNull] this IConventionProperty property, [NotNull] Func valueGeneratorFactory, bool fromDataAnnotation = false) @@ -302,9 +304,9 @@ public static Func SetValueGeneratorFact /// The converter, or to remove any previously set converter. /// Indicates whether the configuration was specified using a data annotation. /// The configured value. - public static ValueConverter SetValueConverter( + public static ValueConverter? SetValueConverter( [NotNull] this IConventionProperty property, - [CanBeNull] ValueConverter converter, + [CanBeNull] ValueConverter? converter, bool fromDataAnnotation = false) => property.AsProperty().SetValueConverter( converter, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); @@ -324,9 +326,9 @@ public static ValueConverter SetValueConverter( /// The type to use, or to remove any previously set type. /// Indicates whether the configuration was specified using a data annotation. /// The configured value. - public static Type SetProviderClrType( + public static Type? SetProviderClrType( [NotNull] this IConventionProperty property, - [CanBeNull] Type providerClrType, + [CanBeNull] Type? providerClrType, bool fromDataAnnotation = false) => property.AsProperty().SetProviderClrType( providerClrType, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); @@ -346,9 +348,9 @@ public static Type SetProviderClrType( /// The comparer, or to remove any previously set comparer. /// Indicates whether the configuration was specified using a data annotation. /// The configured value. - public static ValueComparer SetValueComparer( + public static ValueComparer? SetValueComparer( [NotNull] this IConventionProperty property, - [CanBeNull] ValueComparer comparer, + [CanBeNull] ValueComparer? comparer, bool fromDataAnnotation = false) => property.AsProperty().SetValueComparer( comparer, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); @@ -370,7 +372,7 @@ public static ValueComparer SetValueComparer( [Obsolete("Use SetValueComparer. Only a single value comparer is allowed for a given property.")] public static void SetKeyValueComparer( [NotNull] this IConventionProperty property, - [CanBeNull] ValueComparer comparer, + [CanBeNull] ValueComparer? comparer, bool fromDataAnnotation = false) => property.AsProperty().SetValueComparer( comparer, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); @@ -393,7 +395,7 @@ public static void SetKeyValueComparer( [Obsolete("Use SetValueComparer. Only a single value comparer is allowed for a given property.")] public static void SetStructuralValueComparer( [NotNull] this IConventionProperty property, - [CanBeNull] ValueComparer comparer, + [CanBeNull] ValueComparer? comparer, bool fromDataAnnotation = false) => property.SetKeyValueComparer(comparer, fromDataAnnotation); diff --git a/src/EFCore/Extensions/ConventionTypeBaseExtensions.cs b/src/EFCore/Extensions/ConventionTypeBaseExtensions.cs index c180f59eb34..62363f421ce 100644 --- a/src/EFCore/Extensions/ConventionTypeBaseExtensions.cs +++ b/src/EFCore/Extensions/ConventionTypeBaseExtensions.cs @@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/EntityTypeExtensions.cs b/src/EFCore/Extensions/EntityTypeExtensions.cs index c5cd850250d..d42159a5e1a 100644 --- a/src/EFCore/Extensions/EntityTypeExtensions.cs +++ b/src/EFCore/Extensions/EntityTypeExtensions.cs @@ -16,6 +16,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -138,7 +140,7 @@ public static bool IsAssignableFrom([NotNull] this IEntityType entityType, [NotN /// The closest common parent of and , /// or null if they have not common parent. /// - public static IEntityType GetClosestCommonParent([NotNull] this IEntityType entityType1, [NotNull] IEntityType entityType2) + public static IEntityType? GetClosestCommonParent([NotNull] this IEntityType entityType1, [NotNull] IEntityType entityType2) { Check.NotNull(entityType1, nameof(entityType1)); Check.NotNull(entityType2, nameof(entityType2)); @@ -174,7 +176,7 @@ public static bool IsStrictlyDerivedFrom([NotNull] this IEntityType entityType, /// The least derived type between the specified two. /// If the given entity types are not related, then is returned. /// - public static IEntityType LeastDerivedType([NotNull] this IEntityType entityType, [NotNull] IEntityType otherEntityType) + public static IEntityType? LeastDerivedType([NotNull] this IEntityType entityType, [NotNull] IEntityType otherEntityType) { Check.NotNull(entityType, nameof(entityType)); Check.NotNull(otherEntityType, nameof(otherEntityType)); @@ -203,10 +205,11 @@ public static IEnumerable GetAllBaseTypesInclusiveAscending([NotNul { Check.NotNull(entityType, nameof(entityType)); - while (entityType != null) + var tmp = (IEntityType?)entityType; + while (tmp != null) { - yield return entityType; - entityType = entityType.BaseType; + yield return tmp; + tmp = tmp.BaseType; } } @@ -360,7 +363,8 @@ public static string FullName([NotNull] this ITypeBase type) break; } - root = root.DefiningEntityType; + // TODO-NULLABLE: Put MemberNotNull on DefiningNavigationName (or check HasDefiningNavigation) when we target net5.0 + root = root.DefiningEntityType!; path.Push("#"); path.Push(definingNavigationName); path.Push("."); @@ -452,7 +456,7 @@ public static bool IsInOwnershipPath([NotNull] this IEntityType entityType, [Not /// The entity type. /// The property that the key is defined on. /// The key, or null if none is defined. - public static IKey FindKey([NotNull] this IEntityType entityType, [NotNull] IProperty property) + public static IKey? FindKey([NotNull] this IEntityType entityType, [NotNull] IProperty property) { Check.NotNull(entityType, nameof(entityType)); @@ -501,7 +505,7 @@ public static IEnumerable FindForeignKeys( /// base type of the hierarchy). /// /// The foreign key, or if none is defined. - public static IForeignKey FindForeignKey( + public static IForeignKey? FindForeignKey( [NotNull] this IEntityType entityType, [NotNull] IProperty property, [NotNull] IKey principalKey, @@ -533,7 +537,7 @@ public static IEnumerable GetDeclaredReferencingForeignKeys([NotNul /// /// The entity type. /// The relationship to the owner if this is an owned type or otherwise. - public static IForeignKey FindOwnership([NotNull] this IEntityType entityType) + public static IForeignKey? FindOwnership([NotNull] this IEntityType entityType) => ((EntityType)entityType).FindOwnership(); /// @@ -542,7 +546,7 @@ public static IForeignKey FindOwnership([NotNull] this IEntityType entityType) /// The entity type. /// The navigation property on the entity class. /// The navigation property, or if none is found. - public static INavigation FindNavigation([NotNull] this IEntityType entityType, [NotNull] MemberInfo memberInfo) + public static INavigation? FindNavigation([NotNull] this IEntityType entityType, [NotNull] MemberInfo memberInfo) { Check.NotNull(entityType, nameof(entityType)); Check.NotNull(memberInfo, nameof(memberInfo)); @@ -556,7 +560,7 @@ public static INavigation FindNavigation([NotNull] this IEntityType entityType, /// The entity type. /// The name of the navigation property on the entity class. /// The navigation property, or if none is found. - public static INavigation FindNavigation([NotNull] this IEntityType entityType, [NotNull] string name) + public static INavigation? FindNavigation([NotNull] this IEntityType entityType, [NotNull] string name) => Check.NotNull(entityType, nameof(entityType)).AsEntityType().FindNavigation(Check.NotNull(name, nameof(name))); /// @@ -566,7 +570,7 @@ public static INavigation FindNavigation([NotNull] this IEntityType entityType, /// The entity type. /// The name of the navigation property on the entity class. /// The navigation property, or if none is found. - public static INavigation FindDeclaredNavigation([NotNull] this IEntityType entityType, [NotNull] string name) + public static INavigation? FindDeclaredNavigation([NotNull] this IEntityType entityType, [NotNull] string name) => Check.NotNull(entityType, nameof(entityType)).AsEntityType().FindDeclaredNavigation(Check.NotNull(name, nameof(name))); /// @@ -574,14 +578,15 @@ public static INavigation FindDeclaredNavigation([NotNull] this IEntityType enti /// /// The entity type. /// The defining navigation if one exists or otherwise. - public static INavigation FindDefiningNavigation([NotNull] this IEntityType entityType) + public static INavigation? FindDefiningNavigation([NotNull] this IEntityType entityType) { if (!entityType.HasDefiningNavigation()) { return null; } - var definingNavigation = entityType.DefiningEntityType.FindNavigation(entityType.DefiningNavigationName); + // TODO-NULLABLE: Put two MemberNotNulls on HasDefiningNavigation when we target net5.0 + var definingNavigation = entityType.DefiningEntityType!.FindNavigation(entityType.DefiningNavigationName!); return definingNavigation?.TargetEntityType == entityType ? definingNavigation : null; } @@ -605,7 +610,7 @@ public static IEnumerable GetNavigations([NotNull] this IEntityType /// The entity type. /// The member on the entity class. /// The property, or if none is found. - public static IProperty FindProperty([NotNull] this IEntityType entityType, [NotNull] MemberInfo memberInfo) + public static IProperty? FindProperty([NotNull] this IEntityType entityType, [NotNull] MemberInfo memberInfo) { Check.NotNull(entityType, nameof(entityType)); Check.NotNull(memberInfo, nameof(memberInfo)); @@ -660,7 +665,7 @@ public static IProperty GetProperty([NotNull] this IEntityType entityType, [NotN /// The entity type. /// The property names. /// The properties, or if any property is not found. - public static IReadOnlyList FindProperties( + public static IReadOnlyList? FindProperties( [NotNull] this IEntityType entityType, [NotNull] IReadOnlyList propertyNames) => entityType.AsEntityType().FindProperties(Check.NotNull(propertyNames, nameof(propertyNames))); @@ -672,7 +677,7 @@ public static IReadOnlyList FindProperties( /// The entity type. /// The property name. /// The property, or if none is found. - public static IProperty FindDeclaredProperty([NotNull] this IEntityType entityType, [NotNull] string name) + public static IProperty? FindDeclaredProperty([NotNull] this IEntityType entityType, [NotNull] string name) => entityType.AsEntityType().FindDeclaredProperty(name); /// @@ -686,7 +691,7 @@ public static IProperty FindDeclaredProperty([NotNull] this IEntityType entityTy /// The entity type. /// The property to find the index on. /// The index, or null if none is found. - public static IIndex FindIndex([NotNull] this IEntityType entityType, [NotNull] IProperty property) + public static IIndex? FindIndex([NotNull] this IEntityType entityType, [NotNull] IProperty property) { Check.NotNull(entityType, nameof(entityType)); @@ -709,7 +714,7 @@ public static ChangeTrackingStrategy GetChangeTrackingStrategy([NotNull] this IE /// The entity type. /// If true, then provider values are used. /// The data. - public static IEnumerable> GetSeedData( + public static IEnumerable> GetSeedData( [NotNull] this IEntityType entityType, bool providerValues = false) => entityType.AsEntityType().GetSeedData(providerValues); @@ -719,11 +724,11 @@ public static IEnumerable> GetSeedData( /// /// The entity type to get the query filter for. /// The LINQ expression filter. - public static LambdaExpression GetQueryFilter([NotNull] this IEntityType entityType) + public static LambdaExpression? GetQueryFilter([NotNull] this IEntityType entityType) { Check.NotNull(entityType, nameof(entityType)); - return (LambdaExpression)entityType[CoreAnnotationNames.QueryFilter]; + return (LambdaExpression?)entityType[CoreAnnotationNames.QueryFilter]; } /// @@ -732,25 +737,25 @@ public static LambdaExpression GetQueryFilter([NotNull] this IEntityType entityT /// The entity type to get the defining query for. /// The LINQ query used as the default source. [Obsolete("Use InMemoryEntityTypeExtensions.GetInMemoryQuery")] - public static LambdaExpression GetDefiningQuery([NotNull] this IEntityType entityType) + public static LambdaExpression? GetDefiningQuery([NotNull] this IEntityType entityType) { Check.NotNull(entityType, nameof(entityType)); - return (LambdaExpression)entityType[CoreAnnotationNames.DefiningQuery]; + return (LambdaExpression?)entityType[CoreAnnotationNames.DefiningQuery]; } /// /// Returns the that will be used for storing a discriminator value. /// /// The entity type. - public static IProperty GetDiscriminatorProperty([NotNull] this IEntityType entityType) + public static IProperty? GetDiscriminatorProperty([NotNull] this IEntityType entityType) { if (entityType.BaseType != null) { return entityType.GetRootType().GetDiscriminatorProperty(); } - var propertyName = (string)entityType[CoreAnnotationNames.DiscriminatorProperty]; + var propertyName = (string?)entityType[CoreAnnotationNames.DiscriminatorProperty]; return propertyName == null ? null : entityType.FindProperty(propertyName); } @@ -771,7 +776,7 @@ private static bool GetDefaultIsDiscriminatorMappingComplete(IEntityType entityT /// /// The entity type. /// The discriminator value for this entity type. - public static object GetDiscriminatorValue([NotNull] this IEntityType entityType) + public static object? GetDiscriminatorValue([NotNull] this IEntityType entityType) => entityType[CoreAnnotationNames.DiscriminatorValue]; /// diff --git a/src/EFCore/Extensions/ForeignKeyExtensions.cs b/src/EFCore/Extensions/ForeignKeyExtensions.cs index 840212f126b..24191b2d294 100644 --- a/src/EFCore/Extensions/ForeignKeyExtensions.cs +++ b/src/EFCore/Extensions/ForeignKeyExtensions.cs @@ -11,6 +11,8 @@ using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -32,9 +34,9 @@ public static class ForeignKeyExtensions /// The for which a factory is needed. /// The type of key instanceas. /// A new factory. - public static IDependentKeyValueFactory GetDependentKeyValueFactory( + public static IDependentKeyValueFactory? GetDependentKeyValueFactory( [NotNull] this IForeignKey foreignKey) - => (IDependentKeyValueFactory)foreignKey.AsForeignKey().DependentKeyValueFactory; + => (IDependentKeyValueFactory?)foreignKey.AsForeignKey().DependentKeyValueFactory; /// /// Gets the entity type related to the given one. @@ -67,9 +69,9 @@ public static IEntityType GetRelatedEntityType([NotNull] this IForeignKey foreig /// A value indicating whether the navigation is on the dependent type pointing to the principal type. /// /// - /// A navigation associated with this foreign key or null. + /// A navigation associated with this foreign key or . /// - public static INavigation GetNavigation([NotNull] this IForeignKey foreignKey, bool pointsToPrincipal) + public static INavigation? GetNavigation([NotNull] this IForeignKey foreignKey, bool pointsToPrincipal) => pointsToPrincipal ? foreignKey.DependentToPrincipal : foreignKey.PrincipalToDependent; /// diff --git a/src/EFCore/Extensions/IndexExtensions.cs b/src/EFCore/Extensions/IndexExtensions.cs index fce38e621c5..4d121c3009e 100644 --- a/src/EFCore/Extensions/IndexExtensions.cs +++ b/src/EFCore/Extensions/IndexExtensions.cs @@ -9,6 +9,8 @@ using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/KeyExtensions.cs b/src/EFCore/Extensions/KeyExtensions.cs index 7a48b94cf0b..42176e40ae8 100644 --- a/src/EFCore/Extensions/KeyExtensions.cs +++ b/src/EFCore/Extensions/KeyExtensions.cs @@ -12,6 +12,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/ModelExtensions.cs b/src/EFCore/Extensions/ModelExtensions.cs index 7d582659135..8b98f20c495 100644 --- a/src/EFCore/Extensions/ModelExtensions.cs +++ b/src/EFCore/Extensions/ModelExtensions.cs @@ -12,6 +12,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -29,7 +31,7 @@ public static class ModelExtensions /// The type to find the corresponding entity type for. /// The entity type, or if none if found. [DebuggerStepThrough] - public static IEntityType FindEntityType([NotNull] this IModel model, [NotNull] Type type) + public static IEntityType? FindEntityType([NotNull] this IModel model, [NotNull] Type type) => ((Model)model).FindEntityType(Check.NotNull(type, nameof(type))); /// @@ -41,7 +43,7 @@ public static IEntityType FindEntityType([NotNull] this IModel model, [NotNull] /// The model to find the entity type in. /// The type to find the corresponding entity type for. /// The entity type, or if none if found. - public static IEntityType FindRuntimeEntityType([NotNull] this IModel model, [NotNull] Type type) + public static IEntityType? FindRuntimeEntityType([NotNull] this IModel model, [NotNull] Type type) { Check.NotNull(type, nameof(type)); var realModel = (Model)Check.NotNull(model, nameof(model)); @@ -62,7 +64,7 @@ public static IEntityType FindRuntimeEntityType([NotNull] this IModel model, [No /// The defining entity type of the entity type to find. /// The entity type, or if none are found. [DebuggerStepThrough] - public static IEntityType FindEntityType( + public static IEntityType? FindEntityType( [NotNull] this IModel model, [NotNull] Type type, [NotNull] string definingNavigationName, @@ -162,7 +164,7 @@ public static PropertyAccessMode GetPropertyAccessMode([NotNull] this IModel mod /// Gets the EF Core assembly version used to build this model /// /// The model to get the version for. - public static string GetProductVersion([NotNull] this IModel model) + public static string? GetProductVersion([NotNull] this IModel model) => model[CoreAnnotationNames.ProductVersion] as string; /// diff --git a/src/EFCore/Extensions/MutableAnnotatableExtensions.cs b/src/EFCore/Extensions/MutableAnnotatableExtensions.cs index 4d8c3d0d92b..1577e8d8748 100644 --- a/src/EFCore/Extensions/MutableAnnotatableExtensions.cs +++ b/src/EFCore/Extensions/MutableAnnotatableExtensions.cs @@ -7,6 +7,8 @@ using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -49,7 +51,7 @@ public static void AddAnnotations( public static void SetOrRemoveAnnotation( [NotNull] this IMutableAnnotatable annotatable, [NotNull] string name, - [CanBeNull] object value) + [CanBeNull] object? value) => ((ConventionAnnotatable)annotatable).SetOrRemoveAnnotation(name, value, ConfigurationSource.Explicit); } } diff --git a/src/EFCore/Extensions/MutableEntityTypeExtensions.cs b/src/EFCore/Extensions/MutableEntityTypeExtensions.cs index 42dda10d6ab..4a8fbc01279 100644 --- a/src/EFCore/Extensions/MutableEntityTypeExtensions.cs +++ b/src/EFCore/Extensions/MutableEntityTypeExtensions.cs @@ -13,6 +13,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -155,8 +157,8 @@ public static IEnumerable GetDeclaredIndexes([NotNull] this IMuta /// /// The entity type. /// The name of the property to remove. - /// The property that was removed. - public static IMutableProperty RemoveProperty([NotNull] this IMutableEntityType entityType, [NotNull] string name) + /// The removed property, or if the property was not found. + public static IMutableProperty? RemoveProperty([NotNull] this IMutableEntityType entityType, [NotNull] string name) => ((EntityType)entityType).RemoveProperty(name); /// @@ -166,7 +168,7 @@ public static IMutableProperty RemoveProperty([NotNull] this IMutableEntityType /// The entity type. /// The property that the key is defined on. /// The key, or null if none is defined. - public static IMutableKey FindKey([NotNull] this IMutableEntityType entityType, [NotNull] IProperty property) + public static IMutableKey? FindKey([NotNull] this IMutableEntityType entityType, [NotNull] IProperty property) { Check.NotNull(entityType, nameof(entityType)); @@ -189,8 +191,8 @@ public static IMutableKey AddKey( /// /// The entity type. /// The properties that make up the key. - /// The key that was removed. - public static IMutableKey RemoveKey( + /// The removed key, or if the key was not found. + public static IMutableKey? RemoveKey( [NotNull] this IMutableEntityType entityType, [NotNull] IReadOnlyList properties) => ((EntityType)entityType).RemoveKey(properties); @@ -262,7 +264,7 @@ public static IEnumerable FindForeignKeys( /// base type of the hierarchy). /// /// The foreign key, or if none is defined. - public static IMutableForeignKey FindForeignKey( + public static IMutableForeignKey? FindForeignKey( [NotNull] this IMutableEntityType entityType, [NotNull] IProperty property, [NotNull] IKey principalKey, @@ -330,7 +332,7 @@ public static IMutableForeignKey AddForeignKey( /// /// The entity type. /// The relationship to the owner if this is an owned type or otherwise. - public static IMutableForeignKey FindOwnership([NotNull] this IMutableEntityType entityType) + public static IMutableForeignKey? FindOwnership([NotNull] this IMutableEntityType entityType) => ((EntityType)entityType).FindOwnership(); /// @@ -344,8 +346,8 @@ public static IMutableForeignKey FindOwnership([NotNull] this IMutableEntityType /// is defined on when the relationship targets a derived type in an inheritance hierarchy (since the key is defined on the /// base type of the hierarchy). /// - /// The foreign key that was removed. - public static IMutableForeignKey RemoveForeignKey( + /// The removed foreign key, or if the index was not found. + public static IMutableForeignKey? RemoveForeignKey( [NotNull] this IMutableEntityType entityType, [NotNull] IReadOnlyList properties, [NotNull] IMutableKey principalKey, @@ -358,7 +360,7 @@ public static IMutableForeignKey RemoveForeignKey( /// The entity type. /// The navigation property on the entity class. /// The navigation property, or if none is found. - public static IMutableNavigation FindNavigation( + public static IMutableNavigation? FindNavigation( [NotNull] this IMutableEntityType entityType, [NotNull] MemberInfo memberInfo) => Check.NotNull(entityType, nameof(entityType)) @@ -370,7 +372,7 @@ public static IMutableNavigation FindNavigation( /// The entity type. /// The name of the navigation property on the entity class. /// The navigation property, or if none is found. - public static IMutableNavigation FindNavigation([NotNull] this IMutableEntityType entityType, [NotNull] string name) + public static IMutableNavigation? FindNavigation([NotNull] this IMutableEntityType entityType, [NotNull] string name) => ((EntityType)entityType).FindNavigation(name); /// @@ -380,7 +382,7 @@ public static IMutableNavigation FindNavigation([NotNull] this IMutableEntityTyp /// The entity type. /// The name of the navigation property on the entity class. /// The navigation property, or if none is found. - public static IMutableNavigation FindDeclaredNavigation([NotNull] this IMutableEntityType entityType, [NotNull] string name) + public static IMutableNavigation? FindDeclaredNavigation([NotNull] this IMutableEntityType entityType, [NotNull] string name) => ((EntityType)entityType).FindDeclaredNavigation(Check.NotNull(name, nameof(name))); /// @@ -388,8 +390,8 @@ public static IMutableNavigation FindDeclaredNavigation([NotNull] this IMutableE /// /// The entity type. /// The defining navigation if one exists or otherwise. - public static IMutableNavigation FindDefiningNavigation([NotNull] this IMutableEntityType entityType) - => (IMutableNavigation)((IEntityType)entityType).FindDefiningNavigation(); + public static IMutableNavigation? FindDefiningNavigation([NotNull] this IMutableEntityType entityType) + => (IMutableNavigation?)((IEntityType)entityType).FindDefiningNavigation(); /// /// Gets all navigation properties on the given entity type. @@ -411,7 +413,7 @@ public static IEnumerable GetNavigations([NotNull] this IMut /// The entity type. /// The property on the entity class. /// The property, or if none is found. - public static IMutableProperty FindProperty([NotNull] this IMutableEntityType entityType, [NotNull] PropertyInfo propertyInfo) + public static IMutableProperty? FindProperty([NotNull] this IMutableEntityType entityType, [NotNull] PropertyInfo propertyInfo) { Check.NotNull(entityType, nameof(entityType)); Check.NotNull(propertyInfo, nameof(propertyInfo)); @@ -430,7 +432,7 @@ public static IMutableProperty FindProperty([NotNull] this IMutableEntityType en /// The entity type. /// The property names. /// The properties, or if any property is not found. - public static IReadOnlyList FindProperties( + public static IReadOnlyList? FindProperties( [NotNull] this IMutableEntityType entityType, [NotNull] IReadOnlyList propertyNames) => ((EntityType)entityType).FindProperties(Check.NotNull(propertyNames, nameof(propertyNames))); @@ -442,7 +444,7 @@ public static IReadOnlyList FindProperties( /// The entity type. /// The property name. /// The property, or if none is found. - public static IMutableProperty FindDeclaredProperty([NotNull] this IMutableEntityType entityType, [NotNull] string name) + public static IMutableProperty? FindDeclaredProperty([NotNull] this IMutableEntityType entityType, [NotNull] string name) => ((EntityType)entityType).FindDeclaredProperty(name); /// @@ -466,7 +468,7 @@ public static IMutableProperty AddProperty( public static IMutableProperty AddProperty( [NotNull] this IMutableEntityType entityType, [NotNull] string name) - => ((EntityType)entityType).AddProperty(name, ConfigurationSource.Explicit); + => ((EntityType)entityType).AddProperty(name, ConfigurationSource.Explicit)!; /// /// Adds a property to this entity type. @@ -479,7 +481,7 @@ public static IMutableProperty AddProperty( [NotNull] this IMutableEntityType entityType, [NotNull] string name, [NotNull] Type propertyType) - => ((EntityType)entityType).AddProperty(name, propertyType, ConfigurationSource.Explicit, ConfigurationSource.Explicit); + => ((EntityType)entityType).AddProperty(name, propertyType, ConfigurationSource.Explicit, ConfigurationSource.Explicit)!; /// /// Adds a property backed up by an indexer to this entity type. @@ -511,7 +513,7 @@ public static IMutableProperty AddIndexerProperty( /// The entity type. /// The property to find the index on. /// The index, or null if none is found. - public static IMutableIndex FindIndex([NotNull] this IMutableEntityType entityType, [NotNull] IProperty property) + public static IMutableIndex? FindIndex([NotNull] this IMutableEntityType entityType, [NotNull] IProperty property) { Check.NotNull(entityType, nameof(entityType)); @@ -534,8 +536,8 @@ public static IMutableIndex AddIndex( /// /// The entity type. /// The properties that make up the index. - /// The index that was removed. - public static IMutableIndex RemoveIndex( + /// The removed index, or if the index was not found. + public static IMutableIndex? RemoveIndex( [NotNull] this IMutableEntityType entityType, [NotNull] IReadOnlyList properties) => ((EntityType)entityType).RemoveIndex(properties); @@ -558,7 +560,7 @@ public static void SetChangeTrackingStrategy( /// The LINQ expression filter. public static void SetQueryFilter( [NotNull] this IMutableEntityType entityType, - [CanBeNull] LambdaExpression queryFilter) + [CanBeNull] LambdaExpression? queryFilter) => Check.NotNull(entityType, nameof(entityType)).AsEntityType() .SetQueryFilter(queryFilter, ConfigurationSource.Explicit); @@ -570,7 +572,7 @@ public static void SetQueryFilter( [Obsolete("Use InMemoryEntityTypeExtensions.SetInMemoryQuery")] public static void SetDefiningQuery( [NotNull] this IMutableEntityType entityType, - [CanBeNull] LambdaExpression definingQuery) + [CanBeNull] LambdaExpression? definingQuery) => Check.NotNull(entityType, nameof(entityType)).AsEntityType() .SetDefiningQuery(definingQuery, ConfigurationSource.Explicit); @@ -578,17 +580,17 @@ public static void SetDefiningQuery( /// Returns the that will be used for storing a discriminator value. /// /// The entity type. - public static IMutableProperty GetDiscriminatorProperty([NotNull] this IMutableEntityType entityType) - => (IMutableProperty)((IEntityType)entityType).GetDiscriminatorProperty(); + public static IMutableProperty? GetDiscriminatorProperty([NotNull] this IMutableEntityType entityType) + => (IMutableProperty?)((IEntityType)entityType).GetDiscriminatorProperty(); /// /// Sets the that will be used for storing a discriminator value. /// /// The entity type. /// The property to set. - public static void SetDiscriminatorProperty([NotNull] this IMutableEntityType entityType, [CanBeNull] IProperty property) + public static void SetDiscriminatorProperty([NotNull] this IMutableEntityType entityType, [CanBeNull] IProperty? property) => Check.NotNull(entityType, nameof(entityType)).AsEntityType() - .SetDiscriminatorProperty((Property)property, ConfigurationSource.Explicit); + .SetDiscriminatorProperty((Property?)property, ConfigurationSource.Explicit); /// /// Sets the value indicating whether the discriminator mapping is complete. @@ -607,7 +609,7 @@ public static void SetDiscriminatorMappingComplete([NotNull] this IMutableEntity /// /// The entity type. /// The value to set. - public static void SetDiscriminatorValue([NotNull] this IMutableEntityType entityType, [CanBeNull] object value) + public static void SetDiscriminatorValue([NotNull] this IMutableEntityType entityType, [CanBeNull] object? value) { entityType.AsEntityType().CheckDiscriminatorValue(entityType, value); diff --git a/src/EFCore/Extensions/MutableForeignKeyExtensions.cs b/src/EFCore/Extensions/MutableForeignKeyExtensions.cs index d4199385015..27ae946b4be 100644 --- a/src/EFCore/Extensions/MutableForeignKeyExtensions.cs +++ b/src/EFCore/Extensions/MutableForeignKeyExtensions.cs @@ -4,6 +4,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -33,7 +35,7 @@ public static IMutableEntityType GetRelatedEntityType( /// /// A navigation associated with this foreign key or . /// - public static IMutableNavigation GetNavigation([NotNull] this IMutableForeignKey foreignKey, bool pointsToPrincipal) + public static IMutableNavigation? GetNavigation([NotNull] this IMutableForeignKey foreignKey, bool pointsToPrincipal) => pointsToPrincipal ? foreignKey.DependentToPrincipal : foreignKey.PrincipalToDependent; } } diff --git a/src/EFCore/Extensions/MutableKeyExtensions.cs b/src/EFCore/Extensions/MutableKeyExtensions.cs index b5df1f6a193..6ae3b46606d 100644 --- a/src/EFCore/Extensions/MutableKeyExtensions.cs +++ b/src/EFCore/Extensions/MutableKeyExtensions.cs @@ -6,6 +6,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/MutableModelExtensions.cs b/src/EFCore/Extensions/MutableModelExtensions.cs index cd56ea0f04f..f00b40e1cc9 100644 --- a/src/EFCore/Extensions/MutableModelExtensions.cs +++ b/src/EFCore/Extensions/MutableModelExtensions.cs @@ -9,6 +9,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -25,7 +27,7 @@ public static class MutableModelExtensions /// The model to find the entity type in. /// The type to find the corresponding entity type for. /// The entity type, or if none if found. - public static IMutableEntityType FindEntityType([NotNull] this IMutableModel model, [NotNull] Type type) + public static IMutableEntityType? FindEntityType([NotNull] this IMutableModel model, [NotNull] Type type) => ((Model)model).FindEntityType(type); /// @@ -37,12 +39,12 @@ public static IMutableEntityType FindEntityType([NotNull] this IMutableModel mod /// The defining navigation of the entity type to find. /// The defining entity type of the entity type to find. /// The entity type, or if none are found. - public static IMutableEntityType FindEntityType( + public static IMutableEntityType? FindEntityType( [NotNull] this IMutableModel model, [NotNull] Type type, [NotNull] string definingNavigationName, [NotNull] IMutableEntityType definingEntityType) - => (IMutableEntityType)((IModel)model).FindEntityType(type, definingNavigationName, definingEntityType); + => (IMutableEntityType?)((IModel)model).FindEntityType(type, definingNavigationName, definingEntityType); /// /// Gets the entity types matching the given type. @@ -70,7 +72,7 @@ public static IReadOnlyCollection GetEntityTypes([NotNull] t /// The model to remove the entity type from. /// The entity type to be removed. /// The entity type that was removed. - public static IMutableEntityType RemoveEntityType([NotNull] this IMutableModel model, [NotNull] Type type) + public static IMutableEntityType? RemoveEntityType([NotNull] this IMutableModel model, [NotNull] Type type) { Check.NotNull(model, nameof(model)); Check.NotNull(type, nameof(type)); @@ -86,7 +88,7 @@ public static IMutableEntityType RemoveEntityType([NotNull] this IMutableModel m /// The defining navigation. /// The defining entity type. /// The entity type that was removed. - public static IMutableEntityType RemoveEntityType( + public static IMutableEntityType? RemoveEntityType( [NotNull] this IMutableModel model, [NotNull] Type type, [NotNull] string definingNavigationName, @@ -102,7 +104,7 @@ public static IMutableEntityType RemoveEntityType( /// The model to remove the entity type from. /// The name of the entity type to be removed. /// The entity type that was removed. - public static IMutableEntityType RemoveEntityType( + public static IMutableEntityType? RemoveEntityType( [NotNull] this IMutableModel model, [NotNull] string name) { @@ -120,7 +122,7 @@ public static IMutableEntityType RemoveEntityType( /// The defining navigation. /// The defining entity type. /// The entity type that was removed. - public static IMutableEntityType RemoveEntityType( + public static IMutableEntityType? RemoveEntityType( [NotNull] this IMutableModel model, [NotNull] string name, [NotNull] string definingNavigationName, @@ -144,7 +146,7 @@ public static IMutableEntityType RemoveEntityType( public static IReadOnlyList FindLeastDerivedEntityTypes( [NotNull] this IMutableModel model, [NotNull] Type type, - [CanBeNull] Func condition = null) + [CanBeNull] Func? condition = null) => Check.NotNull((Model)model, nameof(model)) .FindLeastDerivedEntityTypes(type, condition); @@ -154,7 +156,7 @@ public static IReadOnlyList FindLeastDerivedEntityTypes( /// The model to remove the ignored entity type from. /// The ignored entity type to be removed. /// The name of the removed ignored type. - public static string RemoveIgnored([NotNull] this IMutableModel model, [NotNull] Type type) + public static string? RemoveIgnored([NotNull] this IMutableModel model, [NotNull] Type type) => Check.NotNull((Model)model, nameof(model)).RemoveIgnored( Check.NotNull(type, nameof(type))); @@ -206,7 +208,7 @@ public static void SetChangeTrackingStrategy( /// The name of the ignored type. public static string AddIgnored([NotNull] this IMutableModel model, [NotNull] Type type) => Check.NotNull((Model)model, nameof(model)).AddIgnored( - Check.NotNull(type, nameof(type)), ConfigurationSource.Explicit); + Check.NotNull(type, nameof(type)), ConfigurationSource.Explicit)!; /// /// Returns a value indicating whether the entity types using the given type should be configured @@ -239,7 +241,7 @@ public static void AddOwned([NotNull] this IMutableModel model, [NotNull] Type t /// The model to remove the owned type name from. /// The type of the entity type that should not be owned. /// The name of the removed owned type. - public static string RemoveOwned([NotNull] this IMutableModel model, [NotNull] Type type) + public static string? RemoveOwned([NotNull] this IMutableModel model, [NotNull] Type type) => Check.NotNull((Model)model, nameof(model)).RemoveOwned( Check.NotNull(type, nameof(type))); @@ -261,6 +263,6 @@ public static void AddShared([NotNull] this IMutableModel model, [NotNull] Type /// The model to finalize. /// The finalized . public static IModel FinalizeModel([NotNull] this IMutableModel model) - => ((Model)model).FinalizeModel(); + => ((Model)model).FinalizeModel()!; } } diff --git a/src/EFCore/Extensions/MutableNavigationExtensions.cs b/src/EFCore/Extensions/MutableNavigationExtensions.cs index d1f2f936201..f2d3c80a4c5 100644 --- a/src/EFCore/Extensions/MutableNavigationExtensions.cs +++ b/src/EFCore/Extensions/MutableNavigationExtensions.cs @@ -5,6 +5,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/MutablePropertyBaseExtensions.cs b/src/EFCore/Extensions/MutablePropertyBaseExtensions.cs index bbe913d6aef..b5323e8e677 100644 --- a/src/EFCore/Extensions/MutablePropertyBaseExtensions.cs +++ b/src/EFCore/Extensions/MutablePropertyBaseExtensions.cs @@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/MutablePropertyExtensions.cs b/src/EFCore/Extensions/MutablePropertyExtensions.cs index a93f39644d6..e66291d158f 100644 --- a/src/EFCore/Extensions/MutablePropertyExtensions.cs +++ b/src/EFCore/Extensions/MutablePropertyExtensions.cs @@ -12,6 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.ValueGeneration; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -26,8 +28,8 @@ public static class MutablePropertyExtensions /// /// The foreign key property. /// The first associated principal property, or if none exists. - public static IMutableProperty FindFirstPrincipal([NotNull] this IMutableProperty property) - => (IMutableProperty)((IProperty)property).FindFirstPrincipal(); + public static IMutableProperty? FindFirstPrincipal([NotNull] this IMutableProperty property) + => (IMutableProperty?)((IProperty)property).FindFirstPrincipal(); /// /// Finds the list of principal properties including the given property that the given property is constrained by @@ -68,8 +70,8 @@ public static IEnumerable GetContainingIndexes([NotNull] this IMu /// /// The primary that use this property, or if it is not part of the primary key. /// - public static IMutableKey FindContainingPrimaryKey([NotNull] this IMutableProperty property) - => (IMutableKey)((IProperty)property).FindContainingPrimaryKey(); + public static IMutableKey? FindContainingPrimaryKey([NotNull] this IMutableProperty property) + => (IMutableKey?)((IProperty)property).FindContainingPrimaryKey(); /// /// Gets all primary or alternate keys that use this property (including composite keys in which this property @@ -189,7 +191,7 @@ public static void SetValueGeneratorFactory( /// /// The property. /// The converter, or to remove any previously set converter. - public static void SetValueConverter([NotNull] this IMutableProperty property, [CanBeNull] ValueConverter converter) + public static void SetValueConverter([NotNull] this IMutableProperty property, [CanBeNull] ValueConverter? converter) => property.AsProperty().SetValueConverter(converter, ConfigurationSource.Explicit); /// @@ -197,7 +199,7 @@ public static void SetValueConverter([NotNull] this IMutableProperty property, [ /// /// The property. /// The type to use, or to remove any previously set type. - public static void SetProviderClrType([NotNull] this IMutableProperty property, [CanBeNull] Type providerClrType) + public static void SetProviderClrType([NotNull] this IMutableProperty property, [CanBeNull] Type? providerClrType) => property.AsProperty().SetProviderClrType(providerClrType, ConfigurationSource.Explicit); /// @@ -215,7 +217,7 @@ public static CoreTypeMapping SetTypeMapping( /// /// The property. /// The comparer, or to remove any previously set comparer. - public static void SetValueComparer([NotNull] this IMutableProperty property, [CanBeNull] ValueComparer comparer) + public static void SetValueComparer([NotNull] this IMutableProperty property, [CanBeNull] ValueComparer? comparer) => property.AsProperty().SetValueComparer(comparer, ConfigurationSource.Explicit); /// @@ -224,7 +226,7 @@ public static void SetValueComparer([NotNull] this IMutableProperty property, [C /// The property. /// The comparer, or to remove any previously set comparer. [Obsolete("Use SetValueComparer. Only a single value comparer is allowed for a given property.")] - public static void SetKeyValueComparer([NotNull] this IMutableProperty property, [CanBeNull] ValueComparer comparer) + public static void SetKeyValueComparer([NotNull] this IMutableProperty property, [CanBeNull] ValueComparer? comparer) => property.AsProperty().SetValueComparer(comparer, ConfigurationSource.Explicit); /// @@ -233,7 +235,7 @@ public static void SetKeyValueComparer([NotNull] this IMutableProperty property, /// The property. /// The comparer, or to remove any previously set comparer. [Obsolete("Use SetValueComparer. Only a single value comparer is allowed for a given property.")] - public static void SetStructuralValueComparer([NotNull] this IMutableProperty property, [CanBeNull] ValueComparer comparer) + public static void SetStructuralValueComparer([NotNull] this IMutableProperty property, [CanBeNull] ValueComparer? comparer) => property.SetValueComparer(comparer); } } diff --git a/src/EFCore/Extensions/MutableTypeBaseExtensions.cs b/src/EFCore/Extensions/MutableTypeBaseExtensions.cs index e3b196b8770..fa29cc016da 100644 --- a/src/EFCore/Extensions/MutableTypeBaseExtensions.cs +++ b/src/EFCore/Extensions/MutableTypeBaseExtensions.cs @@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/NavigationExtensions.cs b/src/EFCore/Extensions/NavigationExtensions.cs index 18ce9dbf073..17f109c0352 100644 --- a/src/EFCore/Extensions/NavigationExtensions.cs +++ b/src/EFCore/Extensions/NavigationExtensions.cs @@ -10,6 +10,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/PropertyBaseExtensions.cs b/src/EFCore/Extensions/PropertyBaseExtensions.cs index fc63eb113cc..9bc49d90340 100644 --- a/src/EFCore/Extensions/PropertyBaseExtensions.cs +++ b/src/EFCore/Extensions/PropertyBaseExtensions.cs @@ -11,6 +11,8 @@ using Microsoft.EntityFrameworkCore.Update; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -79,7 +81,7 @@ public static IClrPropertyGetter GetGetter([NotNull] this IPropertyBase property /// /// The property for which the backing field will be returned. /// The name of the backing field, or . - public static string GetFieldName([NotNull] this IPropertyBase propertyBase) + public static string? GetFieldName([NotNull] this IPropertyBase propertyBase) => propertyBase.FieldInfo?.GetSimpleMemberName(); /// diff --git a/src/EFCore/Extensions/PropertyExtensions.cs b/src/EFCore/Extensions/PropertyExtensions.cs index 1a0c8075ee7..a497f85e751 100644 --- a/src/EFCore/Extensions/PropertyExtensions.cs +++ b/src/EFCore/Extensions/PropertyExtensions.cs @@ -17,6 +17,8 @@ using Microsoft.EntityFrameworkCore.Utilities; using Microsoft.EntityFrameworkCore.ValueGeneration; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { @@ -47,7 +49,7 @@ public static CoreTypeMapping GetTypeMapping([NotNull] this IProperty property) /// /// The property. /// The type mapping, or if none was found. - public static CoreTypeMapping FindTypeMapping([NotNull] this IProperty property) + public static CoreTypeMapping? FindTypeMapping([NotNull] this IProperty property) => ((Property)property).TypeMapping; /// @@ -56,7 +58,7 @@ public static CoreTypeMapping FindTypeMapping([NotNull] this IProperty property) /// /// The foreign key property. /// The first associated principal property, or if none exists. - public static IProperty FindFirstPrincipal([NotNull] this IProperty property) + public static IProperty? FindFirstPrincipal([NotNull] this IProperty property) { Check.NotNull(property, nameof(property)); @@ -181,7 +183,7 @@ public static IEnumerable GetContainingIndexes([NotNull] this IProperty /// /// The property to get primary key for. /// The primary that use this property, or if it is not part of the primary key. - public static IKey FindContainingPrimaryKey([NotNull] this IProperty property) + public static IKey? FindContainingPrimaryKey([NotNull] this IProperty property) => Check.NotNull((Property)property, nameof(property)).PrimaryKey; /// @@ -294,8 +296,8 @@ public static PropertySaveBehavior GetAfterSaveBehavior([NotNull] this IProperty /// /// The property to get the value generator factory for. /// The factory, or if no factory has been set. - public static Func GetValueGeneratorFactory([NotNull] this IProperty property) - => (Func) + public static Func? GetValueGeneratorFactory([NotNull] this IProperty property) + => (Func?) Check.NotNull(property, nameof(property))[CoreAnnotationNames.ValueGeneratorFactory]; /// @@ -303,27 +305,27 @@ public static Func GetValueGeneratorFact /// /// The property. /// The converter, or if none has been set. - public static ValueConverter GetValueConverter([NotNull] this IProperty property) - => (ValueConverter)Check.NotNull(property, nameof(property))[CoreAnnotationNames.ValueConverter]; + public static ValueConverter? GetValueConverter([NotNull] this IProperty property) + => (ValueConverter?)Check.NotNull(property, nameof(property))[CoreAnnotationNames.ValueConverter]; /// /// Gets the type that the property value will be converted to before being sent to the database provider. /// /// The property. /// The provider type, or if none has been set. - public static Type GetProviderClrType([NotNull] this IProperty property) - => (Type)Check.NotNull(property, nameof(property))[CoreAnnotationNames.ProviderClrType]; + public static Type? GetProviderClrType([NotNull] this IProperty property) + => (Type?)Check.NotNull(property, nameof(property))[CoreAnnotationNames.ProviderClrType]; /// /// Gets the for this property, or if none is set. /// /// The property. /// The comparer, or if none has been set. - public static ValueComparer GetValueComparer([NotNull] this IProperty property) + public static ValueComparer? GetValueComparer([NotNull] this IProperty property) { Check.NotNull(property, nameof(property)); - return (ValueComparer)property[CoreAnnotationNames.ValueComparer] + return (ValueComparer?)property[CoreAnnotationNames.ValueComparer] ?? property.FindTypeMapping()?.Comparer; } @@ -332,11 +334,11 @@ public static ValueComparer GetValueComparer([NotNull] this IProperty property) /// /// The property. /// The comparer, or if none has been set. - public static ValueComparer GetKeyValueComparer([NotNull] this IProperty property) + public static ValueComparer? GetKeyValueComparer([NotNull] this IProperty property) { Check.NotNull(property, nameof(property)); - return (ValueComparer)property[CoreAnnotationNames.ValueComparer] + return (ValueComparer?)property[CoreAnnotationNames.ValueComparer] ?? property.FindTypeMapping()?.KeyComparer; } @@ -346,7 +348,7 @@ public static ValueComparer GetKeyValueComparer([NotNull] this IProperty propert /// The property. /// The comparer, or if none has been set. [Obsolete("Use GetKeyValueComparer. A separate structural comparer is no longer supported.")] - public static ValueComparer GetStructuralValueComparer([NotNull] this IProperty property) + public static ValueComparer? GetStructuralValueComparer([NotNull] this IProperty property) => property.GetKeyValueComparer(); /// @@ -357,7 +359,8 @@ public static ValueComparer GetStructuralValueComparer([NotNull] this IProperty /// A new equality comparer. public static IEqualityComparer CreateKeyEqualityComparer([NotNull] this IProperty property) { - var comparer = property.GetKeyValueComparer(); + // TODO-NULLABLE: #22031 + var comparer = property.GetKeyValueComparer()!; return comparer is IEqualityComparer nullableComparer ? nullableComparer diff --git a/src/EFCore/Extensions/ServicePropertyExtensions.cs b/src/EFCore/Extensions/ServicePropertyExtensions.cs index a0d82a1006d..e3b315dbb18 100644 --- a/src/EFCore/Extensions/ServicePropertyExtensions.cs +++ b/src/EFCore/Extensions/ServicePropertyExtensions.cs @@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/SkipNavigationExtensions.cs b/src/EFCore/Extensions/SkipNavigationExtensions.cs index 21babb46d9a..80e1bf725b6 100644 --- a/src/EFCore/Extensions/SkipNavigationExtensions.cs +++ b/src/EFCore/Extensions/SkipNavigationExtensions.cs @@ -8,6 +8,8 @@ using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Extensions/TypeBaseExtensions.cs b/src/EFCore/Extensions/TypeBaseExtensions.cs index d28509a46a8..9752f1f7b90 100644 --- a/src/EFCore/Extensions/TypeBaseExtensions.cs +++ b/src/EFCore/Extensions/TypeBaseExtensions.cs @@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable once CheckNamespace namespace Microsoft.EntityFrameworkCore { diff --git a/src/EFCore/Infrastructure/AnnotatableExtensions.cs b/src/EFCore/Infrastructure/AnnotatableExtensions.cs index df406f6261d..17cd3b648e5 100644 --- a/src/EFCore/Infrastructure/AnnotatableExtensions.cs +++ b/src/EFCore/Infrastructure/AnnotatableExtensions.cs @@ -8,6 +8,8 @@ using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Infrastructure { /// diff --git a/src/EFCore/Infrastructure/ConventionAnnotatable.cs b/src/EFCore/Infrastructure/ConventionAnnotatable.cs index 4110fde455f..7c36305b7a6 100644 --- a/src/EFCore/Infrastructure/ConventionAnnotatable.cs +++ b/src/EFCore/Infrastructure/ConventionAnnotatable.cs @@ -10,6 +10,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Infrastructure { /// @@ -39,7 +41,7 @@ public abstract class ConventionAnnotatable : Annotatable, IConventionAnnotatabl /// The added annotation. public virtual ConventionAnnotation AddAnnotation( [NotNull] string name, - [CanBeNull] object value, + [CanBeNull] object? value, ConfigurationSource configurationSource) => (ConventionAnnotation)base.AddAnnotation(name, CreateAnnotation(name, value, configurationSource)); @@ -50,9 +52,9 @@ public virtual ConventionAnnotation AddAnnotation( /// The key of the annotation to be added. /// The value to be stored in the annotation. /// The configuration source of the annotation to be set. - public virtual ConventionAnnotation SetAnnotation( + public virtual ConventionAnnotation? SetAnnotation( [NotNull] string name, - [CanBeNull] object value, + [CanBeNull] object? value, ConfigurationSource configurationSource) { var oldAnnotation = FindAnnotation(name); @@ -67,7 +69,7 @@ public virtual ConventionAnnotation SetAnnotation( configurationSource = configurationSource.Max(oldAnnotation.GetConfigurationSource()); } - return (ConventionAnnotation)base.SetAnnotation(name, CreateAnnotation(name, value, configurationSource), oldAnnotation); + return (ConventionAnnotation?)base.SetAnnotation(name, CreateAnnotation(name, value, configurationSource), oldAnnotation); } /// @@ -77,8 +79,8 @@ public virtual ConventionAnnotation SetAnnotation( /// The annotation set. /// The old annotation. /// The annotation that was set. - protected override Annotation OnAnnotationSet(string name, Annotation annotation, Annotation oldAnnotation) - => (Annotation)OnAnnotationSet(name, (IConventionAnnotation)annotation, (IConventionAnnotation)oldAnnotation); + protected override Annotation? OnAnnotationSet(string name, Annotation? annotation, Annotation? oldAnnotation) + => (Annotation?)OnAnnotationSet(name, (IConventionAnnotation?)annotation, (IConventionAnnotation?)oldAnnotation); /// /// Called when an annotation was set or removed. @@ -87,10 +89,10 @@ protected override Annotation OnAnnotationSet(string name, Annotation annotation /// The annotation set. /// The old annotation. /// The annotation that was set. - protected virtual IConventionAnnotation OnAnnotationSet( + protected virtual IConventionAnnotation? OnAnnotationSet( [NotNull] string name, - [CanBeNull] IConventionAnnotation annotation, - [CanBeNull] IConventionAnnotation oldAnnotation) + [CanBeNull] IConventionAnnotation? annotation, + [CanBeNull] IConventionAnnotation? oldAnnotation) => annotation; /// @@ -100,24 +102,24 @@ protected virtual IConventionAnnotation OnAnnotationSet( /// /// The existing annotation if an annotation with the specified name already exists. Otherwise, . /// - public new virtual ConventionAnnotation FindAnnotation([NotNull] string name) - => (ConventionAnnotation)base.FindAnnotation(name); + public new virtual ConventionAnnotation? FindAnnotation([NotNull] string name) + => (ConventionAnnotation?)base.FindAnnotation(name); /// /// Removes the given annotation from this object. /// /// The annotation to remove. /// The annotation that was removed. - public new virtual ConventionAnnotation RemoveAnnotation([NotNull] string name) - => (ConventionAnnotation)base.RemoveAnnotation(name); + public new virtual ConventionAnnotation? RemoveAnnotation([NotNull] string name) + => (ConventionAnnotation?)base.RemoveAnnotation(name); /// - protected override Annotation CreateAnnotation(string name, object value) + protected override Annotation CreateAnnotation(string name, object? value) => CreateAnnotation(name, value, ConfigurationSource.Explicit); private static ConventionAnnotation CreateAnnotation( string name, - object value, + object? value, ConfigurationSource configurationSource) => new ConventionAnnotation(name, value, configurationSource); @@ -132,16 +134,16 @@ IEnumerable IConventionAnnotatable.GetAnnotations() /// [DebuggerStepThrough] - IConventionAnnotation IConventionAnnotatable.SetAnnotation(string name, object value, bool fromDataAnnotation) + IConventionAnnotation? IConventionAnnotatable.SetAnnotation(string name, object? value, bool fromDataAnnotation) => SetAnnotation(name, value, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// [DebuggerStepThrough] - void IMutableAnnotatable.SetAnnotation(string name, object value) + void IMutableAnnotatable.SetAnnotation(string name, object? value) => SetAnnotation(name, value, ConfigurationSource.Explicit); /// - object IMutableAnnotatable.this[string name] + object? IMutableAnnotatable.this[string name] { [DebuggerStepThrough] get => this[name]; @@ -161,17 +163,17 @@ object IMutableAnnotatable.this[string name] /// [DebuggerStepThrough] - IConventionAnnotation IConventionAnnotatable.AddAnnotation(string name, object value, bool fromDataAnnotation) + IConventionAnnotation IConventionAnnotatable.AddAnnotation(string name, object? value, bool fromDataAnnotation) => AddAnnotation(name, value, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// [DebuggerStepThrough] - IConventionAnnotation IConventionAnnotatable.FindAnnotation(string name) + IConventionAnnotation? IConventionAnnotatable.FindAnnotation(string name) => FindAnnotation(name); /// [DebuggerStepThrough] - IConventionAnnotation IConventionAnnotatable.RemoveAnnotation(string name) + IConventionAnnotation? IConventionAnnotatable.RemoveAnnotation(string name) => RemoveAnnotation(name); } } diff --git a/src/EFCore/Metadata/Builders/IConventionPropertyBuilder.cs b/src/EFCore/Metadata/Builders/IConventionPropertyBuilder.cs index 557f1708f34..971be5eadde 100644 --- a/src/EFCore/Metadata/Builders/IConventionPropertyBuilder.cs +++ b/src/EFCore/Metadata/Builders/IConventionPropertyBuilder.cs @@ -334,18 +334,18 @@ bool CanSetValueGenerator( /// The same builder instance if the configuration was applied, /// otherwise. /// - IConventionPropertyBuilder HasTypeMapping([CanBeNull] CoreTypeMapping typeMapping, bool fromDataAnnotation = false); + IConventionPropertyBuilder HasTypeMapping([NotNull] CoreTypeMapping typeMapping, bool fromDataAnnotation = false); /// /// Returns a value indicating whether the given /// can be configured for this property from the current configuration source. /// - /// The type mapping, or to remove any previously set type mapping. + /// The type mapping. /// Indicates whether the configuration was specified using a data annotation. /// /// if the given can be configured for this property. /// - bool CanSetTypeMapping([CanBeNull] CoreTypeMapping typeMapping, bool fromDataAnnotation = false); + bool CanSetTypeMapping([NotNull] CoreTypeMapping typeMapping, bool fromDataAnnotation = false); /// /// Configures the for this property. diff --git a/src/EFCore/Metadata/IConventionAnnotatable.cs b/src/EFCore/Metadata/IConventionAnnotatable.cs index 6028654bc18..8f62a86a894 100644 --- a/src/EFCore/Metadata/IConventionAnnotatable.cs +++ b/src/EFCore/Metadata/IConventionAnnotatable.cs @@ -49,7 +49,7 @@ public interface IConventionAnnotatable : IAnnotatable /// The value to be stored in the annotation. /// Indicates whether the configuration was specified using a data annotation. /// The new annotation. - IConventionAnnotation SetAnnotation([NotNull] string name, [CanBeNull] object? value, bool fromDataAnnotation = false); + IConventionAnnotation? SetAnnotation([NotNull] string name, [CanBeNull] object? value, bool fromDataAnnotation = false); /// /// Gets the annotation with the given name, returning if it does not exist. diff --git a/src/EFCore/Metadata/IConventionEntityType.cs b/src/EFCore/Metadata/IConventionEntityType.cs index 7fd080eaf42..fed58c0705d 100644 --- a/src/EFCore/Metadata/IConventionEntityType.cs +++ b/src/EFCore/Metadata/IConventionEntityType.cs @@ -162,7 +162,7 @@ void HasNoKey(bool? keyless, bool fromDataAnnotation = false) /// Removes a primary or alternate key from this entity type. /// /// The key to be removed. - /// The removed key. + /// The removed key, or if the key was not found. IConventionKey? RemoveKey([NotNull] IConventionKey key); /// @@ -214,7 +214,7 @@ void HasNoKey(bool? keyless, bool fromDataAnnotation = false) /// Removes a foreign key from this entity type. /// /// The foreign key to be removed. - /// The removed foreign key. + /// The removed foreign key, or if the index was not found. IConventionForeignKey? RemoveForeignKey([NotNull] IConventionForeignKey foreignKey); /// @@ -292,7 +292,7 @@ void HasNoKey(bool? keyless, bool fromDataAnnotation = false) /// Removes a skip navigation property from this entity type. /// /// The skip navigation to be removed. - /// The removed skip navigation. + /// The removed skip navigation, or if the skip navigation was not found. IConventionSkipNavigation? RemoveSkipNavigation([NotNull] IConventionSkipNavigation navigation); /// @@ -346,7 +346,7 @@ void HasNoKey(bool? keyless, bool fromDataAnnotation = false) /// Removes an index from this entity type. /// /// The index to remove. - /// The removed index. + /// The removed index, or if the index was not found. IConventionIndex? RemoveIndex([NotNull] IConventionIndex index); /// @@ -403,7 +403,7 @@ void HasNoKey(bool? keyless, bool fromDataAnnotation = false) /// Removes a property from this entity type. /// /// The property to remove. - /// The removed property. + /// The removed property, or if the property was not found. IConventionProperty? RemoveProperty([NotNull] IConventionProperty property); /// diff --git a/src/EFCore/Metadata/IConventionForeignKey.cs b/src/EFCore/Metadata/IConventionForeignKey.cs index adf27884cf5..d6b5240c84d 100644 --- a/src/EFCore/Metadata/IConventionForeignKey.cs +++ b/src/EFCore/Metadata/IConventionForeignKey.cs @@ -8,6 +8,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata.Builders; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -25,7 +27,7 @@ public interface IConventionForeignKey : IForeignKey, IConventionAnnotatable /// /// Gets the builder that can be used to configure this foreign key. /// - new IConventionForeignKeyBuilder Builder { get; } + new IConventionForeignKeyBuilder? Builder { get; } /// /// Gets the foreign key properties in the dependent entity. @@ -54,12 +56,12 @@ public interface IConventionForeignKey : IForeignKey, IConventionAnnotatable /// /// Gets the navigation property on the dependent entity type that points to the principal entity. /// - new IConventionNavigation DependentToPrincipal { get; } + new IConventionNavigation? DependentToPrincipal { get; } /// /// Gets the navigation property on the principal entity type that points to the dependent entity. /// - new IConventionNavigation PrincipalToDependent { get; } + new IConventionNavigation? PrincipalToDependent { get; } /// /// Returns the configuration source for this property. @@ -183,7 +185,7 @@ IReadOnlyList SetProperties( /// /// Indicates whether the configuration was specified using a data annotation. /// The newly created navigation property. - IConventionNavigation SetDependentToPrincipal([CanBeNull] string name, bool fromDataAnnotation = false); + IConventionNavigation? SetDependentToPrincipal([CanBeNull] string? name, bool fromDataAnnotation = false); /// /// Sets the navigation property on the dependent entity type that points to the principal entity. @@ -194,7 +196,7 @@ IReadOnlyList SetProperties( /// /// Indicates whether the configuration was specified using a data annotation. /// The newly created navigation property. - IConventionNavigation SetDependentToPrincipal([CanBeNull] MemberInfo property, bool fromDataAnnotation = false); + IConventionNavigation? SetDependentToPrincipal([CanBeNull] MemberInfo? property, bool fromDataAnnotation = false); /// /// Sets the navigation property on the dependent entity type that points to the principal entity. @@ -206,7 +208,7 @@ IReadOnlyList SetProperties( /// Indicates whether the configuration was specified using a data annotation. /// The newly created navigation property. [Obsolete("Use SetDependentToPrincipal")] - IConventionNavigation HasDependentToPrincipal([CanBeNull] string name, bool fromDataAnnotation = false) + IConventionNavigation? HasDependentToPrincipal([CanBeNull] string? name, bool fromDataAnnotation = false) => SetDependentToPrincipal(name, fromDataAnnotation); /// @@ -219,7 +221,7 @@ IConventionNavigation HasDependentToPrincipal([CanBeNull] string name, bool from /// Indicates whether the configuration was specified using a data annotation. /// The newly created navigation property. [Obsolete("Use SetDependentToPrincipal")] - IConventionNavigation HasDependentToPrincipal([CanBeNull] MemberInfo property, bool fromDataAnnotation = false) + IConventionNavigation? HasDependentToPrincipal([CanBeNull] MemberInfo? property, bool fromDataAnnotation = false) => SetDependentToPrincipal(property, fromDataAnnotation); /// @@ -237,7 +239,7 @@ IConventionNavigation HasDependentToPrincipal([CanBeNull] MemberInfo property, b /// /// Indicates whether the configuration was specified using a data annotation. /// The newly created navigation property. - IConventionNavigation SetPrincipalToDependent([CanBeNull] string name, bool fromDataAnnotation = false); + IConventionNavigation? SetPrincipalToDependent([CanBeNull] string? name, bool fromDataAnnotation = false); /// /// Sets the navigation property on the principal entity type that points to the dependent entity. @@ -248,7 +250,7 @@ IConventionNavigation HasDependentToPrincipal([CanBeNull] MemberInfo property, b /// /// Indicates whether the configuration was specified using a data annotation. /// The newly created navigation property. - IConventionNavigation SetPrincipalToDependent([CanBeNull] MemberInfo property, bool fromDataAnnotation = false); + IConventionNavigation? SetPrincipalToDependent([CanBeNull] MemberInfo? property, bool fromDataAnnotation = false); /// /// Sets the navigation property on the principal entity type that points to the dependent entity. @@ -260,7 +262,7 @@ IConventionNavigation HasDependentToPrincipal([CanBeNull] MemberInfo property, b /// Indicates whether the configuration was specified using a data annotation. /// The newly created navigation property. [Obsolete("Use SetPrincipalToDependent")] - IConventionNavigation HasPrincipalToDependent([CanBeNull] string name, bool fromDataAnnotation = false) + IConventionNavigation? HasPrincipalToDependent([CanBeNull] string? name, bool fromDataAnnotation = false) => SetPrincipalToDependent(name, fromDataAnnotation); /// @@ -273,7 +275,7 @@ IConventionNavigation HasPrincipalToDependent([CanBeNull] string name, bool from /// Indicates whether the configuration was specified using a data annotation. /// The newly created navigation property. [Obsolete("Use SetPrincipalToDependent")] - IConventionNavigation HasPrincipalToDependent([CanBeNull] MemberInfo property, bool fromDataAnnotation = false) + IConventionNavigation? HasPrincipalToDependent([CanBeNull] MemberInfo? property, bool fromDataAnnotation = false) => SetPrincipalToDependent(property, fromDataAnnotation); /// diff --git a/src/EFCore/Metadata/IConventionIndex.cs b/src/EFCore/Metadata/IConventionIndex.cs index fae466eb227..0bfe30d8bc2 100644 --- a/src/EFCore/Metadata/IConventionIndex.cs +++ b/src/EFCore/Metadata/IConventionIndex.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using Microsoft.EntityFrameworkCore.Metadata.Builders; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -20,7 +22,7 @@ public interface IConventionIndex : IIndex, IConventionAnnotatable /// /// Gets the builder that can be used to configure this index. /// - new IConventionIndexBuilder Builder { get; } + new IConventionIndexBuilder? Builder { get; } /// /// Gets the properties that this index is defined on. diff --git a/src/EFCore/Metadata/IConventionKey.cs b/src/EFCore/Metadata/IConventionKey.cs index 427792c1d18..84762589389 100644 --- a/src/EFCore/Metadata/IConventionKey.cs +++ b/src/EFCore/Metadata/IConventionKey.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using Microsoft.EntityFrameworkCore.Metadata.Builders; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -20,7 +22,7 @@ public interface IConventionKey : IConventionAnnotatable, IKey /// /// Gets the builder that can be used to configure this key. /// - new IConventionKeyBuilder Builder { get; } + new IConventionKeyBuilder? Builder { get; } /// /// Gets the properties that make up the key. diff --git a/src/EFCore/Metadata/IConventionModel.cs b/src/EFCore/Metadata/IConventionModel.cs index e0916ed6e1b..b7d6041fb21 100644 --- a/src/EFCore/Metadata/IConventionModel.cs +++ b/src/EFCore/Metadata/IConventionModel.cs @@ -6,6 +6,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata.Builders; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -25,7 +27,7 @@ public interface IConventionModel : IModel, IConventionAnnotatable /// /// Gets the builder that can be used to configure this model. /// - new IConventionModelBuilder Builder { get; } + new IConventionModelBuilder? Builder { get; } /// /// @@ -39,7 +41,7 @@ public interface IConventionModel : IModel, IConventionAnnotatable /// The name of the entity to be added. /// Indicates whether the configuration was specified using a data annotation. /// The new entity type. - IConventionEntityType AddEntityType([NotNull] string name, bool fromDataAnnotation = false); + IConventionEntityType? AddEntityType([NotNull] string name, bool fromDataAnnotation = false); /// /// Adds an entity type to the model. @@ -47,7 +49,7 @@ public interface IConventionModel : IModel, IConventionAnnotatable /// The CLR class that is used to represent instances of the entity type. /// Indicates whether the configuration was specified using a data annotation. /// The new entity type. - IConventionEntityType AddEntityType([NotNull] Type type, bool fromDataAnnotation = false); + IConventionEntityType? AddEntityType([NotNull] Type type, bool fromDataAnnotation = false); /// /// @@ -62,7 +64,7 @@ public interface IConventionModel : IModel, IConventionAnnotatable /// The CLR class that is used to represent instances of the entity type. /// Indicates whether the configuration was specified using a data annotation. /// The new entity type. - IConventionEntityType AddEntityType([NotNull] string name, [NotNull] Type clrType, bool fromDataAnnotation = false); + IConventionEntityType? AddEntityType([NotNull] string name, [NotNull] Type clrType, bool fromDataAnnotation = false); /// /// Adds an entity type with a defining navigation to the model. @@ -72,7 +74,7 @@ public interface IConventionModel : IModel, IConventionAnnotatable /// The defining entity type. /// Indicates whether the configuration was specified using a data annotation. /// The new entity type. - IConventionEntityType AddEntityType( + IConventionEntityType? AddEntityType( [NotNull] string name, [NotNull] string definingNavigationName, [NotNull] IConventionEntityType definingEntityType, @@ -86,7 +88,7 @@ IConventionEntityType AddEntityType( /// The defining entity type. /// Indicates whether the configuration was specified using a data annotation. /// The new entity type. - IConventionEntityType AddEntityType( + IConventionEntityType? AddEntityType( [NotNull] Type type, [NotNull] string definingNavigationName, [NotNull] IConventionEntityType definingEntityType, @@ -99,7 +101,7 @@ IConventionEntityType AddEntityType( /// /// The name of the entity type to find. /// The entity type, or if none are found. - new IConventionEntityType FindEntityType([NotNull] string name); + new IConventionEntityType? FindEntityType([NotNull] string name); /// /// Gets the entity type for the given name, defining navigation name @@ -109,7 +111,7 @@ IConventionEntityType AddEntityType( /// The defining navigation of the entity type to find. /// The defining entity type of the entity type to find. /// The entity type, or if none are found. - IConventionEntityType FindEntityType( + IConventionEntityType? FindEntityType( [NotNull] string name, [NotNull] string definingNavigationName, [NotNull] IConventionEntityType definingEntityType); @@ -118,8 +120,8 @@ IConventionEntityType FindEntityType( /// Removes an entity type from the model. /// /// The entity type to be removed. - /// The removed entity type. - IConventionEntityType RemoveEntityType([NotNull] IConventionEntityType entityType); + /// The removed entity type, or if the entity type was not found. + IConventionEntityType? RemoveEntityType([NotNull] IConventionEntityType entityType); /// /// Gets all entity types defined in the model. @@ -133,14 +135,14 @@ IConventionEntityType FindEntityType( /// The name of the entity type to be ignored. /// Indicates whether the configuration was specified using a data annotation. /// The name of the ignored entity type. - string AddIgnored([NotNull] string typeName, bool fromDataAnnotation = false); + string? AddIgnored([NotNull] string typeName, bool fromDataAnnotation = false); /// /// Removes the ignored entity type name. /// /// The name of the ignored entity type to be removed. /// The removed ignored type name. - string RemoveIgnored([NotNull] string typeName); + string? RemoveIgnored([NotNull] string typeName); /// /// Gets whether the CLR type is used by shared type entities in the model. diff --git a/src/EFCore/Metadata/IConventionNavigation.cs b/src/EFCore/Metadata/IConventionNavigation.cs index d5c7a6326db..a512e3a0b6f 100644 --- a/src/EFCore/Metadata/IConventionNavigation.cs +++ b/src/EFCore/Metadata/IConventionNavigation.cs @@ -6,6 +6,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata.Builders; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -22,7 +24,7 @@ public interface IConventionNavigation : INavigation, IConventionNavigationBase /// /// Gets the builder that can be used to configure this navigation. /// - new IConventionNavigationBuilder Builder { get; } + new IConventionNavigationBuilder? Builder { get; } /// /// Gets the type that this navigation property belongs to. @@ -49,7 +51,7 @@ public interface IConventionNavigation : INavigation, IConventionNavigationBase ConfigurationSource IConventionPropertyBase.GetConfigurationSource() => (ConfigurationSource)(IsOnDependent ? ForeignKey.GetDependentToPrincipalConfigurationSource() - : ForeignKey.GetPrincipalToDependentConfigurationSource()); + : ForeignKey.GetPrincipalToDependentConfigurationSource())!; /// /// Gets the foreign key that defines the relationship this navigation property will navigate. @@ -78,7 +80,7 @@ ConfigurationSource IConventionPropertyBase.GetConfigurationSource() /// /// Indicates whether the configuration was specified using a data annotation. /// The new inverse navigation. - IConventionNavigation SetInverse([CanBeNull] string inverseName, bool fromDataAnnotation = false); + IConventionNavigation? SetInverse([CanBeNull] string? inverseName, bool fromDataAnnotation = false); /// /// Sets the inverse navigation. @@ -89,7 +91,7 @@ ConfigurationSource IConventionPropertyBase.GetConfigurationSource() /// /// Indicates whether the configuration was specified using a data annotation. /// The new inverse navigation. - IConventionNavigation SetInverse([CanBeNull] MemberInfo inverse, bool fromDataAnnotation = false); + IConventionNavigation? SetInverse([CanBeNull] MemberInfo? inverse, bool fromDataAnnotation = false); /// /// Returns the configuration source for . diff --git a/src/EFCore/Metadata/IConventionNavigationBase.cs b/src/EFCore/Metadata/IConventionNavigationBase.cs index 8514f68f1c1..a6d856274fb 100644 --- a/src/EFCore/Metadata/IConventionNavigationBase.cs +++ b/src/EFCore/Metadata/IConventionNavigationBase.cs @@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// diff --git a/src/EFCore/Metadata/IConventionProperty.cs b/src/EFCore/Metadata/IConventionProperty.cs index 641fde0ef87..b8dffb5af87 100644 --- a/src/EFCore/Metadata/IConventionProperty.cs +++ b/src/EFCore/Metadata/IConventionProperty.cs @@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -19,7 +21,7 @@ public interface IConventionProperty : IProperty, IConventionPropertyBase /// /// Gets the builder that can be used to configure this property. /// - new IConventionPropertyBuilder Builder { get; } + new IConventionPropertyBuilder? Builder { get; } /// /// Gets the type that this property belongs to. diff --git a/src/EFCore/Metadata/IConventionSkipNavigation.cs b/src/EFCore/Metadata/IConventionSkipNavigation.cs index 858620083da..6bc69004340 100644 --- a/src/EFCore/Metadata/IConventionSkipNavigation.cs +++ b/src/EFCore/Metadata/IConventionSkipNavigation.cs @@ -5,6 +5,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata.Builders; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -22,7 +24,7 @@ public interface IConventionSkipNavigation : ISkipNavigation, IConventionNavigat /// /// Gets the builder that can be used to configure this property. /// - new IConventionSkipNavigationBuilder Builder { get; } + new IConventionSkipNavigationBuilder? Builder { get; } /// /// Gets the type that this navigation property belongs to. @@ -45,19 +47,19 @@ public interface IConventionSkipNavigation : ISkipNavigation, IConventionNavigat /// /// Gets the join type used by the foreign key. /// - new IConventionEntityType JoinEntityType + new IConventionEntityType? JoinEntityType { [DebuggerStepThrough] - get => (IConventionEntityType)((ISkipNavigation)this).JoinEntityType; + get => (IConventionEntityType?)((ISkipNavigation)this).JoinEntityType; } /// /// Gets the foreign key to the join type. /// - new IConventionForeignKey ForeignKey + new IConventionForeignKey? ForeignKey { [DebuggerStepThrough] - get => (IConventionForeignKey)((ISkipNavigation)this).ForeignKey; + get => (IConventionForeignKey?)((ISkipNavigation)this).ForeignKey; } /// @@ -68,7 +70,7 @@ public interface IConventionSkipNavigation : ISkipNavigation, IConventionNavigat /// /// Indicates whether the configuration was specified using a data annotation. /// The new foreign key. - IConventionForeignKey SetForeignKey([CanBeNull] IConventionForeignKey foreignKey, bool fromDataAnnotation = false); + IConventionForeignKey? SetForeignKey([CanBeNull] IConventionForeignKey? foreignKey, bool fromDataAnnotation = false); /// /// Returns the configuration source for . @@ -79,10 +81,10 @@ public interface IConventionSkipNavigation : ISkipNavigation, IConventionNavigat /// /// Gets the inverse skip navigation. /// - new IConventionSkipNavigation Inverse + new IConventionSkipNavigation? Inverse { [DebuggerStepThrough] - get => (IConventionSkipNavigation)((ISkipNavigation)this).Inverse; + get => (IConventionSkipNavigation?)((ISkipNavigation)this).Inverse; } /// @@ -93,7 +95,7 @@ public interface IConventionSkipNavigation : ISkipNavigation, IConventionNavigat /// /// Indicates whether the configuration was specified using a data annotation. [DebuggerStepThrough] - IConventionSkipNavigation SetInverse([CanBeNull] IConventionSkipNavigation inverse, bool fromDataAnnotation = false); + IConventionSkipNavigation? SetInverse([CanBeNull] IConventionSkipNavigation? inverse, bool fromDataAnnotation = false); /// /// Returns the configuration source for . diff --git a/src/EFCore/Metadata/IForeignKey.cs b/src/EFCore/Metadata/IForeignKey.cs index 5c6326b310c..9db25bc2cc8 100644 --- a/src/EFCore/Metadata/IForeignKey.cs +++ b/src/EFCore/Metadata/IForeignKey.cs @@ -5,6 +5,8 @@ using System.Linq; using Microsoft.EntityFrameworkCore.Infrastructure; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -40,12 +42,12 @@ public interface IForeignKey : IAnnotatable /// /// Gets the navigation property on the dependent entity type that points to the principal entity. /// - INavigation DependentToPrincipal { get; } + INavigation? DependentToPrincipal { get; } /// /// Gets the navigation property on the principal entity type that points to the dependent entity. /// - INavigation PrincipalToDependent { get; } + INavigation? PrincipalToDependent { get; } /// /// Gets a value indicating whether the values assigned to the foreign key properties are unique. diff --git a/src/EFCore/Metadata/IIndex.cs b/src/EFCore/Metadata/IIndex.cs index cf2bce5f05f..453cc2daf4f 100644 --- a/src/EFCore/Metadata/IIndex.cs +++ b/src/EFCore/Metadata/IIndex.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using Microsoft.EntityFrameworkCore.Infrastructure; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -19,7 +21,7 @@ public interface IIndex : IAnnotatable /// /// Gets the name of this index. /// - string Name { get; } + string? Name { get; } /// /// Gets a value indicating whether the values assigned to the indexed properties are unique. diff --git a/src/EFCore/Metadata/IKey.cs b/src/EFCore/Metadata/IKey.cs index 772dcfdccef..a2a7a1ee832 100644 --- a/src/EFCore/Metadata/IKey.cs +++ b/src/EFCore/Metadata/IKey.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using Microsoft.EntityFrameworkCore.Infrastructure; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// diff --git a/src/EFCore/Metadata/IModel.cs b/src/EFCore/Metadata/IModel.cs index 44b97d36711..34e293d7526 100644 --- a/src/EFCore/Metadata/IModel.cs +++ b/src/EFCore/Metadata/IModel.cs @@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -37,7 +39,7 @@ public interface IModel : IAnnotatable /// /// The name of the entity type to find. /// The entity type, or null if none are found. - IEntityType FindEntityType([NotNull] string name); + IEntityType? FindEntityType([NotNull] string name); /// /// Gets the entity type for the given name, defining navigation name @@ -47,7 +49,7 @@ public interface IModel : IAnnotatable /// The defining navigation of the entity type to find. /// The defining entity type of the entity type to find. /// The entity type, or null if none are found. - IEntityType FindEntityType( + IEntityType? FindEntityType( [NotNull] string name, [NotNull] string definingNavigationName, [NotNull] IEntityType definingEntityType); diff --git a/src/EFCore/Metadata/IMutableEntityType.cs b/src/EFCore/Metadata/IMutableEntityType.cs index 0ce3efd5dc5..94a1a0b60f2 100644 --- a/src/EFCore/Metadata/IMutableEntityType.cs +++ b/src/EFCore/Metadata/IMutableEntityType.cs @@ -70,7 +70,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// /// The properties that make up the alternate key. /// The newly created key. - IMutableKey? AddKey([NotNull] IReadOnlyList properties); + IMutableKey AddKey([NotNull] IReadOnlyList properties); /// /// Gets the primary or alternate key that is defined on the given properties. @@ -90,7 +90,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// Removes a primary or alternate key from this entity type. /// /// The key to be removed. - /// The removed key. + /// The removed key, or if the key was not found. IMutableKey? RemoveKey([NotNull] IMutableKey key); /// @@ -104,7 +104,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// base type of the hierarchy). /// /// The newly created foreign key. - IMutableForeignKey? AddForeignKey( + IMutableForeignKey AddForeignKey( [NotNull] IReadOnlyList properties, [NotNull] IMutableKey principalKey, [NotNull] IMutableEntityType principalEntityType); @@ -136,7 +136,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// Removes a foreign key from this entity type. /// /// The foreign key to be removed. - /// The removed foreign key. + /// The removed foreign key, or if the index was not found. IMutableForeignKey? RemoveForeignKey([NotNull] IMutableForeignKey foreignKey); /// @@ -157,7 +157,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// Whether the navigation property is defined on the dependent side of the underlying foreign key. /// /// The newly created skip navigation property. - IMutableSkipNavigation? AddSkipNavigation( + IMutableSkipNavigation AddSkipNavigation( [NotNull] string name, [CanBeNull] MemberInfo? memberInfo, [NotNull] IMutableEntityType targetEntityType, @@ -212,7 +212,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// Removes a skip navigation properties from this entity type. /// /// The skip navigation to be removed. - /// The removed skip navigation. + /// The removed skip navigation, or if the skip navigation was not found. IMutableSkipNavigation? RemoveSkipNavigation([NotNull] IMutableSkipNavigation navigation); /// @@ -220,7 +220,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// /// The properties that are to be indexed. /// The newly created index. - IMutableIndex? AddIndex([NotNull] IReadOnlyList properties); + IMutableIndex AddIndex([NotNull] IReadOnlyList properties); /// /// Adds a named index to this entity type. @@ -228,7 +228,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// The properties that are to be indexed. /// The name of the index. /// The newly created index. - IMutableIndex? AddIndex( + IMutableIndex AddIndex( [NotNull] IReadOnlyList properties, [NotNull] string name); @@ -261,7 +261,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// Removes an index from this entity type. /// /// The index to remove. - /// The removed index. + /// The removed index, or if the index was not found. IMutableIndex? RemoveIndex([NotNull] IMutableIndex index); /// @@ -278,7 +278,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// /// /// The newly created property. - IMutableProperty? AddProperty([NotNull] string name, [NotNull] Type propertyType, [CanBeNull] MemberInfo? memberInfo); + IMutableProperty AddProperty([NotNull] string name, [NotNull] Type propertyType, [CanBeNull] MemberInfo? memberInfo); /// /// @@ -311,7 +311,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// Removes a property from this entity type. /// /// The property to remove. - /// The removed property. + /// The removed property, or if the property was not found. IMutableProperty? RemoveProperty([NotNull] IMutableProperty property); /// @@ -319,7 +319,7 @@ public interface IMutableEntityType : IEntityType, IMutableTypeBase /// /// The or of the property to add. /// The newly created property. - IMutableServiceProperty? AddServiceProperty([NotNull] MemberInfo memberInfo); + IMutableServiceProperty AddServiceProperty([NotNull] MemberInfo memberInfo); /// /// diff --git a/src/EFCore/Metadata/IMutableForeignKey.cs b/src/EFCore/Metadata/IMutableForeignKey.cs index 826f8ecf467..bc674a0250e 100644 --- a/src/EFCore/Metadata/IMutableForeignKey.cs +++ b/src/EFCore/Metadata/IMutableForeignKey.cs @@ -7,6 +7,8 @@ using System.Reflection; using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -77,12 +79,12 @@ public interface IMutableForeignKey : IForeignKey, IMutableAnnotatable /// /// Gets the navigation property on the dependent entity type that points to the principal entity. /// - new IMutableNavigation DependentToPrincipal { get; } + new IMutableNavigation? DependentToPrincipal { get; } /// /// Gets the navigation property on the principal entity type that points to the dependent entity. /// - new IMutableNavigation PrincipalToDependent { get; } + new IMutableNavigation? PrincipalToDependent { get; } /// /// Sets the foreign key properties and that target principal key. @@ -99,7 +101,7 @@ public interface IMutableForeignKey : IForeignKey, IMutableAnnotatable /// no navigation property defined. /// /// The newly set navigation property. - IMutableNavigation SetDependentToPrincipal([CanBeNull] string name); + IMutableNavigation? SetDependentToPrincipal([CanBeNull] string name); /// /// Sets the navigation property on the dependent entity type that points to the principal entity. @@ -109,7 +111,7 @@ public interface IMutableForeignKey : IForeignKey, IMutableAnnotatable /// no navigation property defined. /// /// The newly set navigation property. - IMutableNavigation SetDependentToPrincipal([CanBeNull] MemberInfo property); + IMutableNavigation? SetDependentToPrincipal([CanBeNull] MemberInfo property); /// /// Sets the navigation property on the dependent entity type that points to the principal entity. @@ -120,7 +122,7 @@ public interface IMutableForeignKey : IForeignKey, IMutableAnnotatable /// /// The newly created navigation property. [Obsolete("Use SetDependentToPrincipal")] - IMutableNavigation HasDependentToPrincipal([CanBeNull] string name) + IMutableNavigation? HasDependentToPrincipal([CanBeNull] string name) => SetDependentToPrincipal(name); /// @@ -132,7 +134,7 @@ IMutableNavigation HasDependentToPrincipal([CanBeNull] string name) /// /// The newly created navigation property. [Obsolete("Use SetDependentToPrincipal")] - IMutableNavigation HasDependentToPrincipal([CanBeNull] MemberInfo property) + IMutableNavigation? HasDependentToPrincipal([CanBeNull] MemberInfo property) => SetDependentToPrincipal(property); /// @@ -143,7 +145,7 @@ IMutableNavigation HasDependentToPrincipal([CanBeNull] MemberInfo property) /// no navigation property defined. /// /// The newly set navigation property. - IMutableNavigation SetPrincipalToDependent([CanBeNull] string name); + IMutableNavigation? SetPrincipalToDependent([CanBeNull] string name); /// /// Sets the navigation property on the principal entity type that points to the dependent entity. @@ -153,7 +155,7 @@ IMutableNavigation HasDependentToPrincipal([CanBeNull] MemberInfo property) /// no navigation property defined. /// /// The newly set navigation property. - IMutableNavigation SetPrincipalToDependent([CanBeNull] MemberInfo property); + IMutableNavigation? SetPrincipalToDependent([CanBeNull] MemberInfo property); /// /// Sets the navigation property on the principal entity type that points to the dependent entity. @@ -164,7 +166,7 @@ IMutableNavigation HasDependentToPrincipal([CanBeNull] MemberInfo property) /// /// The newly created navigation property. [Obsolete("Use SetPrincipalToDependent")] - IMutableNavigation HasPrincipalToDependent([CanBeNull] string name) + IMutableNavigation? HasPrincipalToDependent([CanBeNull] string name) => SetPrincipalToDependent(name); /// @@ -176,7 +178,7 @@ IMutableNavigation HasPrincipalToDependent([CanBeNull] string name) /// /// The newly created navigation property. [Obsolete("Use SetPrincipalToDependent")] - IMutableNavigation HasPrincipalToDependent([CanBeNull] MemberInfo property) + IMutableNavigation? HasPrincipalToDependent([CanBeNull] MemberInfo property) => SetPrincipalToDependent(property); /// diff --git a/src/EFCore/Metadata/IMutableIndex.cs b/src/EFCore/Metadata/IMutableIndex.cs index 9bf805b39a0..bfce47e9129 100644 --- a/src/EFCore/Metadata/IMutableIndex.cs +++ b/src/EFCore/Metadata/IMutableIndex.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// diff --git a/src/EFCore/Metadata/IMutableKey.cs b/src/EFCore/Metadata/IMutableKey.cs index b14549d6dcc..e5f7254ed9c 100644 --- a/src/EFCore/Metadata/IMutableKey.cs +++ b/src/EFCore/Metadata/IMutableKey.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// diff --git a/src/EFCore/Metadata/IMutableModel.cs b/src/EFCore/Metadata/IMutableModel.cs index 100a0facdf7..c805c9b6076 100644 --- a/src/EFCore/Metadata/IMutableModel.cs +++ b/src/EFCore/Metadata/IMutableModel.cs @@ -5,6 +5,8 @@ using System.Collections.Generic; using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -86,7 +88,7 @@ IMutableEntityType AddEntityType( /// /// The name of the entity type to find. /// The entity type, or if none are found. - new IMutableEntityType FindEntityType([NotNull] string name); + new IMutableEntityType? FindEntityType([NotNull] string name); /// /// Gets the entity type for the given name, defining navigation name @@ -96,7 +98,7 @@ IMutableEntityType AddEntityType( /// The defining navigation of the entity type to find. /// The defining entity type of the entity type to find. /// The entity type, or if none are found. - IMutableEntityType FindEntityType( + IMutableEntityType? FindEntityType( [NotNull] string name, [NotNull] string definingNavigationName, [NotNull] IMutableEntityType definingEntityType); @@ -105,8 +107,8 @@ IMutableEntityType FindEntityType( /// Removes an entity type from the model. /// /// The entity type to be removed. - /// The removed entity type. - IMutableEntityType RemoveEntityType([NotNull] IMutableEntityType entityType); + /// The removed entity type, or if the entity type was not found. + IMutableEntityType? RemoveEntityType([NotNull] IMutableEntityType entityType); /// /// Gets all entity types defined in the model. @@ -126,7 +128,7 @@ IMutableEntityType FindEntityType( /// /// The name of the ignored entity type to be removed. /// The removed ignored type name. - string RemoveIgnored([NotNull] string typeName); + string? RemoveIgnored([NotNull] string typeName); /// /// Indicates whether the given entity type name is ignored. diff --git a/src/EFCore/Metadata/IMutableNavigation.cs b/src/EFCore/Metadata/IMutableNavigation.cs index 1de9ef2d369..e3762c731f1 100644 --- a/src/EFCore/Metadata/IMutableNavigation.cs +++ b/src/EFCore/Metadata/IMutableNavigation.cs @@ -5,6 +5,8 @@ using System.Reflection; using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -62,7 +64,7 @@ public interface IMutableNavigation : INavigation, IMutableNavigationBase /// no inverse navigation property defined. /// /// The inverse navigation. - IMutableNavigation SetInverse([CanBeNull] string inverseName); + IMutableNavigation? SetInverse([CanBeNull] string? inverseName); /// /// Sets the inverse navigation. @@ -72,6 +74,6 @@ public interface IMutableNavigation : INavigation, IMutableNavigationBase /// no inverse navigation property defined. /// /// The inverse navigation. - IMutableNavigation SetInverse([CanBeNull] MemberInfo inverse); + IMutableNavigation? SetInverse([CanBeNull] MemberInfo? inverse); } } diff --git a/src/EFCore/Metadata/IMutableNavigationBase.cs b/src/EFCore/Metadata/IMutableNavigationBase.cs index ce2b09532f1..8f5d7a2d10a 100644 --- a/src/EFCore/Metadata/IMutableNavigationBase.cs +++ b/src/EFCore/Metadata/IMutableNavigationBase.cs @@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// diff --git a/src/EFCore/Metadata/IMutablePropertyBase.cs b/src/EFCore/Metadata/IMutablePropertyBase.cs index cf07c742e9a..f4c74e9c472 100644 --- a/src/EFCore/Metadata/IMutablePropertyBase.cs +++ b/src/EFCore/Metadata/IMutablePropertyBase.cs @@ -5,6 +5,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -27,7 +29,7 @@ public interface IMutablePropertyBase : IPropertyBase, IMutableAnnotatable /// Gets or sets the underlying CLR field for this property. /// This may be for shadow properties or if the backing field for the property is not known. /// - new FieldInfo FieldInfo { get; [param: CanBeNull] set; } + new FieldInfo? FieldInfo { get; [param: CanBeNull] set; } /// /// @@ -47,7 +49,7 @@ public interface IMutablePropertyBase : IPropertyBase, IMutableAnnotatable /// /// /// The name of the field to use. - void SetField([CanBeNull] string fieldName) + void SetField([CanBeNull] string? fieldName) => this.AsPropertyBase().SetField(fieldName, ConfigurationSource.Explicit); } } diff --git a/src/EFCore/Metadata/IMutableServiceProperty.cs b/src/EFCore/Metadata/IMutableServiceProperty.cs index 31ac51b506a..47d3bc55085 100644 --- a/src/EFCore/Metadata/IMutableServiceProperty.cs +++ b/src/EFCore/Metadata/IMutableServiceProperty.cs @@ -3,6 +3,8 @@ using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -25,6 +27,6 @@ public interface IMutableServiceProperty : IServiceProperty, IMutablePropertyBas /// /// Gets or sets for this property. /// - new ServiceParameterBinding ParameterBinding { get; [param: CanBeNull] set; } + new ServiceParameterBinding? ParameterBinding { get; [param: CanBeNull] set; } } } diff --git a/src/EFCore/Metadata/IMutableSkipNavigation.cs b/src/EFCore/Metadata/IMutableSkipNavigation.cs index 0a80d80257b..58468c9f1da 100644 --- a/src/EFCore/Metadata/IMutableSkipNavigation.cs +++ b/src/EFCore/Metadata/IMutableSkipNavigation.cs @@ -4,6 +4,8 @@ using System.Diagnostics; using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -39,19 +41,19 @@ public interface IMutableSkipNavigation : ISkipNavigation, IMutableNavigationBas /// /// Gets the join type used by the foreign key. /// - new IMutableEntityType JoinEntityType + new IMutableEntityType? JoinEntityType { [DebuggerStepThrough] - get => (IMutableEntityType)((ISkipNavigation)this).JoinEntityType; + get => (IMutableEntityType?)((ISkipNavigation)this).JoinEntityType; } /// /// Gets the foreign key to the join type. /// - new IMutableForeignKey ForeignKey + new IMutableForeignKey? ForeignKey { [DebuggerStepThrough] - get => (IMutableForeignKey)((ISkipNavigation)this).ForeignKey; + get => (IMutableForeignKey?)((ISkipNavigation)this).ForeignKey; } /// @@ -60,15 +62,15 @@ public interface IMutableSkipNavigation : ISkipNavigation, IMutableNavigationBas /// /// The foreign key. Passing will result in there being no foreign key associated. /// - void SetForeignKey([CanBeNull] IMutableForeignKey foreignKey); + void SetForeignKey([CanBeNull] IMutableForeignKey? foreignKey); /// /// Gets the inverse skip navigation. /// - new IMutableSkipNavigation Inverse + new IMutableSkipNavigation? Inverse { [DebuggerStepThrough] - get => (IMutableSkipNavigation)((ISkipNavigation)this).Inverse; + get => (IMutableSkipNavigation?)((ISkipNavigation)this).Inverse; } /// @@ -78,6 +80,6 @@ public interface IMutableSkipNavigation : ISkipNavigation, IMutableNavigationBas /// The inverse skip navigation. Passing will result in there being no inverse navigation property defined. /// [DebuggerStepThrough] - IMutableSkipNavigation SetInverse([CanBeNull] IMutableSkipNavigation inverse); + IMutableSkipNavigation? SetInverse([CanBeNull] IMutableSkipNavigation? inverse); } } diff --git a/src/EFCore/Metadata/IMutableTypeBase.cs b/src/EFCore/Metadata/IMutableTypeBase.cs index be0381bb29b..26b0936338c 100644 --- a/src/EFCore/Metadata/IMutableTypeBase.cs +++ b/src/EFCore/Metadata/IMutableTypeBase.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -34,8 +36,8 @@ public interface IMutableTypeBase : ITypeBase, IMutableAnnotatable /// Removes the ignored member name. /// /// The name of the member to be removed. - /// The removed ignored member name. - string RemoveIgnored([NotNull] string memberName); + /// The removed ignored member name, or if the member name was not found. + string? RemoveIgnored([NotNull] string memberName); /// /// Indicates whether the given member name is ignored. diff --git a/src/EFCore/Metadata/IPropertyBase.cs b/src/EFCore/Metadata/IPropertyBase.cs index 708391036a8..97853ead3b4 100644 --- a/src/EFCore/Metadata/IPropertyBase.cs +++ b/src/EFCore/Metadata/IPropertyBase.cs @@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -32,13 +34,13 @@ public interface IPropertyBase : IAnnotatable /// Gets the for the underlying CLR property for this property-like object. /// This may be for shadow properties or if mapped directly to a field. /// - PropertyInfo PropertyInfo { get; } + PropertyInfo? PropertyInfo { get; } /// /// Gets the for the underlying CLR field for this property-like object. /// This may be for shadow properties or if the backing field is not known. /// - FieldInfo FieldInfo { get; } + FieldInfo? FieldInfo { get; } /// /// diff --git a/src/EFCore/Metadata/ISkipNavigation.cs b/src/EFCore/Metadata/ISkipNavigation.cs index c643780473e..488fb6d03ec 100644 --- a/src/EFCore/Metadata/ISkipNavigation.cs +++ b/src/EFCore/Metadata/ISkipNavigation.cs @@ -4,6 +4,8 @@ using System.Diagnostics; using Microsoft.EntityFrameworkCore.Metadata.Internal; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata { /// @@ -15,7 +17,7 @@ public interface ISkipNavigation : INavigationBase /// /// Gets the join type used by the foreign key. /// - IEntityType JoinEntityType + IEntityType? JoinEntityType => IsOnDependent ? ForeignKey?.PrincipalEntityType : ForeignKey?.DeclaringEntityType; /// diff --git a/src/EFCore/Metadata/Internal/ConventionAnnotatableExtensions.cs b/src/EFCore/Metadata/Internal/ConventionAnnotatableExtensions.cs index 9ec35cdc747..83e9551236b 100644 --- a/src/EFCore/Metadata/Internal/ConventionAnnotatableExtensions.cs +++ b/src/EFCore/Metadata/Internal/ConventionAnnotatableExtensions.cs @@ -4,6 +4,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Infrastructure; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -20,10 +22,10 @@ public static class ConventionAnnotatableExtensions /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static ConventionAnnotation SetOrRemoveAnnotation( + public static ConventionAnnotation? SetOrRemoveAnnotation( [NotNull] this ConventionAnnotatable annotatable, [NotNull] string name, - [CanBeNull] object value, + [CanBeNull] object? value, ConfigurationSource configurationSource) { if (value == null) diff --git a/src/EFCore/Metadata/Internal/ConventionAnnotation.cs b/src/EFCore/Metadata/Internal/ConventionAnnotation.cs index 9cfefbeb6c0..d5e08781262 100644 --- a/src/EFCore/Metadata/Internal/ConventionAnnotation.cs +++ b/src/EFCore/Metadata/Internal/ConventionAnnotation.cs @@ -4,6 +4,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Infrastructure; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -22,7 +24,7 @@ public class ConventionAnnotation : Annotation, IConventionAnnotation /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public ConventionAnnotation([NotNull] string name, [CanBeNull] object value, ConfigurationSource configurationSource) + public ConventionAnnotation([NotNull] string name, [CanBeNull] object? value, ConfigurationSource configurationSource) : base(name, value) { _configurationSource = configurationSource; diff --git a/src/EFCore/Metadata/Internal/CoreAnnotationNames.cs b/src/EFCore/Metadata/Internal/CoreAnnotationNames.cs index 92eb1467cda..d6fd6a9a4dd 100644 --- a/src/EFCore/Metadata/Internal/CoreAnnotationNames.cs +++ b/src/EFCore/Metadata/Internal/CoreAnnotationNames.cs @@ -4,6 +4,8 @@ using System; using System.Collections.Generic; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// diff --git a/src/EFCore/Metadata/Internal/EntityType.cs b/src/EFCore/Metadata/Internal/EntityType.cs index 030090bf4ad..2ed88d3d21a 100644 --- a/src/EFCore/Metadata/Internal/EntityType.cs +++ b/src/EFCore/Metadata/Internal/EntityType.cs @@ -543,8 +543,8 @@ public override string ToString() /// The annotation that was set. protected override IConventionAnnotation OnAnnotationSet( string name, - IConventionAnnotation annotation, - IConventionAnnotation oldAnnotation) + IConventionAnnotation? annotation, + IConventionAnnotation? oldAnnotation) => Model.ConventionDispatcher.OnEntityTypeAnnotationChanged(Builder, name, annotation, oldAnnotation); /// @@ -1031,7 +1031,7 @@ public virtual void OnForeignKeyUpdating([NotNull] ForeignKey foreignKey) } } - removed = foreignKey.PrincipalKey.ReferencingForeignKeys.Remove(foreignKey); + removed = foreignKey.PrincipalKey.ReferencingForeignKeys!.Remove(foreignKey); Check.DebugAssert(removed, "removed is false"); removed = foreignKey.PrincipalEntityType.DeclaredReferencingForeignKeys!.Remove(foreignKey); Check.DebugAssert(removed, "removed is false"); @@ -1903,7 +1903,9 @@ public virtual IEnumerable FindSkipNavigationsInHierarchy([NotNu var removed = _skipNavigations.Remove(navigation.Name); Check.DebugAssert(removed, "Expected the navigation to be removed"); - removed = navigation.ForeignKey?.ReferencingSkipNavigations.Remove(navigation) ?? true; + removed = navigation.ForeignKey is ForeignKey foreignKey + ? foreignKey.ReferencingSkipNavigations!.Remove(navigation) + : true; Check.DebugAssert(removed, "removed is false"); removed = navigation.TargetEntityType.DeclaredReferencingSkipNavigations!.Remove(navigation); @@ -3265,19 +3267,22 @@ private void CheckDiscriminatorProperty(Property? property) /// public virtual void CheckDiscriminatorValue([NotNull] IEntityType entityType, [CanBeNull] object? value) { - if (value != null - && entityType.GetDiscriminatorProperty() == null) + if (value is null) + { + return; + } + + var discriminatorProperty = entityType.GetDiscriminatorProperty(); + if (discriminatorProperty is null) { throw new InvalidOperationException( CoreStrings.NoDiscriminatorForValue(entityType.DisplayName(), entityType.GetRootType().DisplayName())); } - if (value != null - && !entityType.GetDiscriminatorProperty().ClrType.IsAssignableFrom(value.GetType())) + if (!discriminatorProperty.ClrType.IsAssignableFrom(value.GetType())) { throw new InvalidOperationException( - CoreStrings.DiscriminatorValueIncompatible( - value, entityType.GetDiscriminatorProperty().Name, entityType.GetDiscriminatorProperty().ClrType)); + CoreStrings.DiscriminatorValueIncompatible(value, discriminatorProperty.Name, discriminatorProperty.ClrType)); } } @@ -3518,8 +3523,8 @@ IConventionModel IConventionEntityType.Model /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - IMutableKey? IMutableEntityType.AddKey(IReadOnlyList properties) - => AddKey(properties.Cast().ToList(), ConfigurationSource.Explicit); + IMutableKey IMutableEntityType.AddKey(IReadOnlyList properties) + => AddKey(properties.Cast().ToList(), ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -3620,7 +3625,7 @@ IEnumerable IConventionEntityType.GetKeys() /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - IMutableForeignKey? IMutableEntityType.AddForeignKey( + IMutableForeignKey IMutableEntityType.AddForeignKey( IReadOnlyList properties, IMutableKey principalKey, IMutableEntityType principalEntityType) @@ -3629,7 +3634,7 @@ IEnumerable IConventionEntityType.GetKeys() (Key)principalKey, (EntityType)principalEntityType, ConfigurationSource.Explicit, - ConfigurationSource.Explicit); + ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -3749,7 +3754,7 @@ IEnumerable IConventionEntityType.GetForeignKeys() /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - IMutableSkipNavigation? IMutableEntityType.AddSkipNavigation( + IMutableSkipNavigation IMutableEntityType.AddSkipNavigation( string name, MemberInfo? memberInfo, IMutableEntityType targetEntityType, @@ -3757,7 +3762,7 @@ IEnumerable IConventionEntityType.GetForeignKeys() bool onDependent) => AddSkipNavigation( name, memberInfo, (EntityType)targetEntityType, collection, onDependent, - ConfigurationSource.Explicit); + ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -3894,8 +3899,8 @@ IEnumerable IConventionEntityType.GetSkipNavigations( /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - IMutableIndex? IMutableEntityType.AddIndex(IReadOnlyList properties) - => AddIndex(properties.Cast().ToList(), ConfigurationSource.Explicit); + IMutableIndex IMutableEntityType.AddIndex(IReadOnlyList properties) + => AddIndex(properties.Cast().ToList(), ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -3904,8 +3909,8 @@ IEnumerable IConventionEntityType.GetSkipNavigations( /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - IMutableIndex? IMutableEntityType.AddIndex(IReadOnlyList properties, string name) - => AddIndex(properties.Cast().ToList(), name, ConfigurationSource.Explicit); + IMutableIndex IMutableEntityType.AddIndex(IReadOnlyList properties, string name) + => AddIndex(properties.Cast().ToList(), name, ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -4052,10 +4057,10 @@ IEnumerable IConventionEntityType.GetIndexes() /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - IMutableProperty? IMutableEntityType.AddProperty(string name, Type propertyType, MemberInfo? memberInfo) + IMutableProperty IMutableEntityType.AddProperty(string name, Type propertyType, MemberInfo? memberInfo) => AddProperty( name, propertyType, memberInfo ?? ClrType?.GetMembersInHierarchy(name).FirstOrDefault(), - ConfigurationSource.Explicit, ConfigurationSource.Explicit); + ConfigurationSource.Explicit, ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to diff --git a/src/EFCore/Metadata/Internal/EntityTypeExtensions.cs b/src/EFCore/Metadata/Internal/EntityTypeExtensions.cs index cb67c23ea4c..d7423590aa2 100644 --- a/src/EFCore/Metadata/Internal/EntityTypeExtensions.cs +++ b/src/EFCore/Metadata/Internal/EntityTypeExtensions.cs @@ -12,6 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + // ReSharper disable ArgumentsStyleOther // ReSharper disable ArgumentsStyleNamedExpression namespace Microsoft.EntityFrameworkCore.Metadata.Internal @@ -51,7 +53,7 @@ public static MemberInfo GetNavigationMemberInfo( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static IForeignKey FindDeclaredOwnership([NotNull] this IEntityType entityType) + public static IForeignKey? FindDeclaredOwnership([NotNull] this IEntityType entityType) => ((EntityType)entityType).FindDeclaredOwnership(); /// @@ -60,7 +62,7 @@ public static IForeignKey FindDeclaredOwnership([NotNull] this IEntityType entit /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static IEntityType FindInDefinitionPath([NotNull] this IEntityType entityType, [NotNull] Type targetType) + public static IEntityType? FindInDefinitionPath([NotNull] this IEntityType entityType, [NotNull] Type targetType) { var root = entityType; while (root != null) @@ -82,8 +84,8 @@ public static IEntityType FindInDefinitionPath([NotNull] this IEntityType entity /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static EntityType FindInDefinitionPath([NotNull] this EntityType entityType, [NotNull] Type targetType) - => (EntityType)((IEntityType)entityType).FindInDefinitionPath(targetType); + public static EntityType? FindInDefinitionPath([NotNull] this EntityType entityType, [NotNull] Type targetType) + => (EntityType?)((IEntityType)entityType).FindInDefinitionPath(targetType); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -91,7 +93,7 @@ public static EntityType FindInDefinitionPath([NotNull] this EntityType entityTy /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static IEntityType FindInDefinitionPath([NotNull] this IEntityType entityType, [NotNull] string targetTypeName) + public static IEntityType? FindInDefinitionPath([NotNull] this IEntityType entityType, [NotNull] string targetTypeName) { var root = entityType; while (root != null) @@ -113,8 +115,8 @@ public static IEntityType FindInDefinitionPath([NotNull] this IEntityType entity /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static EntityType FindInDefinitionPath([NotNull] this EntityType entityType, [NotNull] string targetTypeName) - => (EntityType)((IEntityType)entityType).FindInDefinitionPath(targetTypeName); + public static EntityType? FindInDefinitionPath([NotNull] this EntityType entityType, [NotNull] string targetTypeName) + => (EntityType?)((IEntityType)entityType).FindInDefinitionPath(targetTypeName); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -140,7 +142,7 @@ public static bool IsInDefinitionPath([NotNull] this IEntityType entityType, [No /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static IEntityType FindInOwnershipPath([NotNull] this IEntityType entityType, [NotNull] Type targetType) + public static IEntityType? FindInOwnershipPath([NotNull] this IEntityType entityType, [NotNull] Type targetType) { var owner = entityType; while (true) @@ -152,7 +154,7 @@ public static IEntityType FindInOwnershipPath([NotNull] this IEntityType entityT } owner = ownership.PrincipalEntityType; - if (owner.ClrType.IsAssignableFrom(targetType)) + if (owner.ClrType?.IsAssignableFrom(targetType) == true) { return owner; } @@ -345,8 +347,8 @@ public static Func GetEmptyShadowValuesFactory([NotNull] this IEntity /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static EntityType LeastDerivedType([NotNull] this EntityType entityType, [NotNull] EntityType otherEntityType) - => (EntityType)((IEntityType)entityType).LeastDerivedType(otherEntityType); + public static EntityType? LeastDerivedType([NotNull] this EntityType entityType, [NotNull] EntityType otherEntityType) + => (EntityType?)((IEntityType)entityType).LeastDerivedType(otherEntityType); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -354,7 +356,7 @@ public static EntityType LeastDerivedType([NotNull] this EntityType entityType, /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static IKey FindDeclaredPrimaryKey([NotNull] this IEntityType entityType) + public static IKey? FindDeclaredPrimaryKey([NotNull] this IEntityType entityType) => entityType.BaseType == null ? entityType.FindPrimaryKey() : null; /// @@ -405,7 +407,7 @@ public static IEnumerable GetPropertiesAndNavigations( /// public static IEnumerable GetNotificationProperties( [NotNull] this IEntityType entityType, - [CanBeNull] string propertyName) + [CanBeNull] string? propertyName) { if (string.IsNullOrEmpty(propertyName)) { @@ -430,7 +432,7 @@ public static IEnumerable GetNotificationProperties( // ReSharper disable once AssignNullToNotNullAttribute var property = entityType.FindProperty(propertyName) ?? entityType.FindNavigation(propertyName) - ?? (IPropertyBase)entityType.FindSkipNavigation(propertyName); + ?? (IPropertyBase?)entityType.FindSkipNavigation(propertyName); if (property != null) { diff --git a/src/EFCore/Metadata/Internal/ForeignKey.cs b/src/EFCore/Metadata/Internal/ForeignKey.cs index 1d22fe254db..2635b7ac777 100644 --- a/src/EFCore/Metadata/Internal/ForeignKey.cs +++ b/src/EFCore/Metadata/Internal/ForeignKey.cs @@ -13,6 +13,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -117,7 +119,7 @@ public ForeignKey( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual InternalForeignKeyBuilder Builder + public virtual InternalForeignKeyBuilder? Builder { get; @@ -131,7 +133,7 @@ public virtual InternalForeignKeyBuilder Builder /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual SortedSet ReferencingSkipNavigations { get; [param: CanBeNull] set; } + public virtual SortedSet? ReferencingSkipNavigations { get; [param: CanBeNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -175,9 +177,9 @@ public virtual void UpdateConfigurationSource(ConfigurationSource configurationS /// The annotation that was set. protected override IConventionAnnotation OnAnnotationSet( string name, - IConventionAnnotation annotation, - IConventionAnnotation oldAnnotation) - => Builder.ModelBuilder.Metadata.ConventionDispatcher.OnForeignKeyAnnotationChanged(Builder, name, annotation, oldAnnotation); + IConventionAnnotation? annotation, + IConventionAnnotation? oldAnnotation) + => Builder!.ModelBuilder.Metadata.ConventionDispatcher.OnForeignKeyAnnotationChanged(Builder, name, annotation, oldAnnotation); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -293,7 +295,7 @@ public virtual void UpdatePrincipalEndConfigurationSource(ConfigurationSource co /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Navigation DependentToPrincipal { [DebuggerStepThrough] get; private set; } + public virtual Navigation? DependentToPrincipal { [DebuggerStepThrough] get; private set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -301,8 +303,8 @@ public virtual void UpdatePrincipalEndConfigurationSource(ConfigurationSource co /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Navigation SetDependentToPrincipal( - [CanBeNull] string name, + public virtual Navigation? SetDependentToPrincipal( + [CanBeNull] string? name, ConfigurationSource configurationSource) => Navigation(MemberIdentity.Create(name), configurationSource, pointsToPrincipal: true); @@ -312,8 +314,8 @@ public virtual Navigation SetDependentToPrincipal( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Navigation SetDependentToPrincipal( - [CanBeNull] MemberInfo property, + public virtual Navigation? SetDependentToPrincipal( + [CanBeNull] MemberInfo? property, ConfigurationSource configurationSource) => Navigation(MemberIdentity.Create(property), configurationSource, pointsToPrincipal: true); @@ -342,7 +344,7 @@ public virtual void UpdateDependentToPrincipalConfigurationSource(ConfigurationS /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Navigation PrincipalToDependent { [DebuggerStepThrough] get; private set; } + public virtual Navigation? PrincipalToDependent { [DebuggerStepThrough] get; private set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -350,8 +352,8 @@ public virtual void UpdateDependentToPrincipalConfigurationSource(ConfigurationS /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Navigation HasPrincipalToDependent( - [CanBeNull] string name, + public virtual Navigation? HasPrincipalToDependent( + [CanBeNull] string? name, ConfigurationSource configurationSource) => Navigation(MemberIdentity.Create(name), configurationSource, pointsToPrincipal: false); @@ -361,8 +363,8 @@ public virtual Navigation HasPrincipalToDependent( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Navigation HasPrincipalToDependent( - [CanBeNull] MemberInfo property, + public virtual Navigation? HasPrincipalToDependent( + [CanBeNull] MemberInfo? property, ConfigurationSource configurationSource) => Navigation(MemberIdentity.Create(property), configurationSource, pointsToPrincipal: false); @@ -391,7 +393,7 @@ public virtual void UpdatePrincipalToDependentConfigurationSource(ConfigurationS /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - private Navigation Navigation( + private Navigation? Navigation( MemberIdentity? propertyIdentity, ConfigurationSource configurationSource, bool pointsToPrincipal) @@ -423,7 +425,7 @@ private Navigation Navigation( UpdatePrincipalToDependentConfigurationSource(configurationSource); } - return oldNavigation; + return oldNavigation!; } if (oldNavigation != null) @@ -440,7 +442,7 @@ private Navigation Navigation( } } - Navigation navigation = null; + Navigation? navigation = null; var property = propertyIdentity?.MemberInfo; if (property != null) { @@ -473,16 +475,16 @@ private Navigation Navigation( if (pointsToPrincipal) { DeclaringEntityType.Model.ConventionDispatcher.OnNavigationRemoved( - DeclaringEntityType.Builder, - PrincipalEntityType.Builder, + DeclaringEntityType.Builder!, + PrincipalEntityType.Builder!, oldNavigation.Name, oldNavigation.GetIdentifyingMemberInfo()); } else { DeclaringEntityType.Model.ConventionDispatcher.OnNavigationRemoved( - PrincipalEntityType.Builder, - DeclaringEntityType.Builder, + PrincipalEntityType.Builder!, + DeclaringEntityType.Builder!, oldNavigation.Name, oldNavigation.GetIdentifyingMemberInfo()); } @@ -490,7 +492,7 @@ private Navigation Navigation( if (navigation != null) { - navigation = (Navigation)DeclaringEntityType.Model.ConventionDispatcher.OnNavigationAdded(navigation.Builder)?.Metadata; + navigation = (Navigation?)DeclaringEntityType.Model.ConventionDispatcher.OnNavigationAdded(navigation.Builder)?.Metadata; } return navigation ?? oldNavigation; @@ -528,6 +530,7 @@ public virtual bool IsUnique if (unique.HasValue && PrincipalEntityType.ClrType != null + && DeclaringEntityType.ClrType != null && PrincipalToDependent != null) { if (!Internal.Navigation.IsCompatible( @@ -537,10 +540,11 @@ public virtual bool IsUnique !unique, shouldThrow: false)) { + // TODO-NULLABLE: Bug if PropertyInfo is null (FieldInfo?) throw new InvalidOperationException( CoreStrings.UnableToSetIsUnique( unique.Value, - PrincipalToDependent.PropertyInfo.Name, + PrincipalToDependent.Name, PrincipalEntityType.DisplayName())); } } @@ -831,7 +835,7 @@ public virtual EntityType ResolveOtherEntityType([NotNull] EntityType entityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual object DependentKeyValueFactory { get; [param: NotNull] set; } + public virtual object? DependentKeyValueFactory { get; [param: NotNull] set; } // Note: This is set and used only by IdentityMapFactoryFactory, which ensures thread-safety /// @@ -840,7 +844,7 @@ public virtual EntityType ResolveOtherEntityType([NotNull] EntityType entityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Func DependentsMapFactory { get; [param: NotNull] set; } + public virtual Func? DependentsMapFactory { get; [param: NotNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -892,7 +896,7 @@ IEntityType IForeignKey.PrincipalEntityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - INavigation IForeignKey.DependentToPrincipal + INavigation? IForeignKey.DependentToPrincipal { [DebuggerStepThrough] get => DependentToPrincipal; } @@ -903,7 +907,7 @@ INavigation IForeignKey.DependentToPrincipal /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - INavigation IForeignKey.PrincipalToDependent + INavigation? IForeignKey.PrincipalToDependent { [DebuggerStepThrough] get => PrincipalToDependent; } @@ -958,7 +962,7 @@ IMutableEntityType IMutableForeignKey.PrincipalEntityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IMutableNavigation IMutableForeignKey.DependentToPrincipal + IMutableNavigation? IMutableForeignKey.DependentToPrincipal { [DebuggerStepThrough] get => DependentToPrincipal; } @@ -969,7 +973,7 @@ IMutableNavigation IMutableForeignKey.DependentToPrincipal /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IMutableNavigation IMutableForeignKey.PrincipalToDependent + IMutableNavigation? IMutableForeignKey.PrincipalToDependent { [DebuggerStepThrough] get => PrincipalToDependent; } @@ -990,7 +994,7 @@ void IMutableForeignKey.SetProperties(IReadOnlyList properties /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IMutableNavigation IMutableForeignKey.SetDependentToPrincipal(string name) + IMutableNavigation? IMutableForeignKey.SetDependentToPrincipal(string name) => SetDependentToPrincipal(name, ConfigurationSource.Explicit); /// @@ -1000,7 +1004,7 @@ IMutableNavigation IMutableForeignKey.SetDependentToPrincipal(string name) /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - IMutableNavigation IMutableForeignKey.SetDependentToPrincipal(MemberInfo property) + IMutableNavigation? IMutableForeignKey.SetDependentToPrincipal(MemberInfo property) => SetDependentToPrincipal(property, ConfigurationSource.Explicit); /// @@ -1010,7 +1014,7 @@ IMutableNavigation IMutableForeignKey.SetDependentToPrincipal(MemberInfo propert /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - IMutableNavigation IMutableForeignKey.SetPrincipalToDependent(string name) + IMutableNavigation? IMutableForeignKey.SetPrincipalToDependent(string name) => HasPrincipalToDependent(name, ConfigurationSource.Explicit); /// @@ -1020,7 +1024,7 @@ IMutableNavigation IMutableForeignKey.SetPrincipalToDependent(string name) /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - IMutableNavigation IMutableForeignKey.SetPrincipalToDependent(MemberInfo property) + IMutableNavigation? IMutableForeignKey.SetPrincipalToDependent(MemberInfo property) => HasPrincipalToDependent(property, ConfigurationSource.Explicit); /// @@ -1073,7 +1077,7 @@ IReadOnlyList IConventionForeignKey.Properties /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionNavigation IConventionForeignKey.DependentToPrincipal + IConventionNavigation? IConventionForeignKey.DependentToPrincipal { [DebuggerStepThrough] get => DependentToPrincipal; } @@ -1084,7 +1088,7 @@ IConventionNavigation IConventionForeignKey.DependentToPrincipal /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionNavigation IConventionForeignKey.PrincipalToDependent + IConventionNavigation? IConventionForeignKey.PrincipalToDependent { [DebuggerStepThrough] get => PrincipalToDependent; } @@ -1095,7 +1099,7 @@ IConventionNavigation IConventionForeignKey.PrincipalToDependent /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionForeignKeyBuilder IConventionForeignKey.Builder + IConventionForeignKeyBuilder? IConventionForeignKey.Builder { [DebuggerStepThrough] get => Builder; } @@ -1106,7 +1110,7 @@ IConventionForeignKeyBuilder IConventionForeignKey.Builder /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionAnnotatableBuilder IConventionAnnotatable.Builder + IConventionAnnotatableBuilder? IConventionAnnotatable.Builder { [DebuggerStepThrough] get => Builder; } @@ -1123,22 +1127,22 @@ IReadOnlyList IConventionForeignKey.SetProperties( /// [DebuggerStepThrough] - IConventionNavigation IConventionForeignKey.SetDependentToPrincipal(string name, bool fromDataAnnotation) + IConventionNavigation? IConventionForeignKey.SetDependentToPrincipal(string? name, bool fromDataAnnotation) => SetDependentToPrincipal(name, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// [DebuggerStepThrough] - IConventionNavigation IConventionForeignKey.SetDependentToPrincipal(MemberInfo property, bool fromDataAnnotation) + IConventionNavigation? IConventionForeignKey.SetDependentToPrincipal(MemberInfo? property, bool fromDataAnnotation) => SetDependentToPrincipal(property, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// [DebuggerStepThrough] - IConventionNavigation IConventionForeignKey.SetPrincipalToDependent(string name, bool fromDataAnnotation) + IConventionNavigation? IConventionForeignKey.SetPrincipalToDependent(string? name, bool fromDataAnnotation) => HasPrincipalToDependent(name, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// [DebuggerStepThrough] - IConventionNavigation IConventionForeignKey.SetPrincipalToDependent(MemberInfo property, bool fromDataAnnotation) + IConventionNavigation? IConventionForeignKey.SetPrincipalToDependent(MemberInfo? property, bool fromDataAnnotation) => HasPrincipalToDependent(property, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// @@ -1258,10 +1262,10 @@ private void Validate( public static bool AreCompatible( [NotNull] EntityType principalEntityType, [NotNull] EntityType dependentEntityType, - [CanBeNull] MemberInfo navigationToPrincipal, - [CanBeNull] MemberInfo navigationToDependent, - [CanBeNull] IReadOnlyList dependentProperties, - [CanBeNull] IReadOnlyList principalProperties, + [CanBeNull] MemberInfo? navigationToPrincipal, + [CanBeNull] MemberInfo? navigationToDependent, + [CanBeNull] IReadOnlyList? dependentProperties, + [CanBeNull] IReadOnlyList? principalProperties, bool? unique, bool shouldThrow) { @@ -1283,8 +1287,8 @@ public static bool AreCompatible( if (navigationToPrincipal != null && !Internal.Navigation.IsCompatible( navigationToPrincipal, - dependentEntityType.ClrType, - principalEntityType.ClrType, + dependentEntityType.ClrType!, + principalEntityType.ClrType!, shouldBeCollection: false, shouldThrow: shouldThrow)) { @@ -1294,8 +1298,8 @@ public static bool AreCompatible( if (navigationToDependent != null && !Internal.Navigation.IsCompatible( navigationToDependent, - principalEntityType.ClrType, - dependentEntityType.ClrType, + principalEntityType.ClrType!, + dependentEntityType.ClrType!, shouldBeCollection: !unique, shouldThrow: shouldThrow)) { diff --git a/src/EFCore/Metadata/Internal/ForeignKeyExtensions.cs b/src/EFCore/Metadata/Internal/ForeignKeyExtensions.cs index 7f16aa8ce67..b5441062686 100644 --- a/src/EFCore/Metadata/Internal/ForeignKeyExtensions.cs +++ b/src/EFCore/Metadata/Internal/ForeignKeyExtensions.cs @@ -8,6 +8,8 @@ using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; using Microsoft.EntityFrameworkCore.Diagnostics; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -243,7 +245,7 @@ public static IEntityType ResolveEntityTypeInHierarchy([NotNull] this IForeignKe /// doing so can result in application failures when updating to a new Entity Framework Core release. /// public static IDependentsMap CreateDependentsMapFactory([NotNull] this IForeignKey foreignKey) - => foreignKey.AsForeignKey().DependentsMapFactory(); + => foreignKey.AsForeignKey().DependentsMapFactory!(); // TODO-NULLABLE: #22031 /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to diff --git a/src/EFCore/Metadata/Internal/Index.cs b/src/EFCore/Metadata/Internal/Index.cs index 09dc1786c01..c599dce1bee 100644 --- a/src/EFCore/Metadata/Internal/Index.cs +++ b/src/EFCore/Metadata/Internal/Index.cs @@ -11,6 +11,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -27,7 +29,7 @@ public class Index : ConventionAnnotatable, IMutableIndex, IConventionIndex private ConfigurationSource? _isUniqueConfigurationSource; // Warning: Never access these fields directly as access needs to be thread-safe - private object _nullableValueFactory; + private object? _nullableValueFactory; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -83,7 +85,7 @@ public Index( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string Name { [DebuggerStepThrough] get; } + public virtual string? Name { [DebuggerStepThrough] get; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -99,7 +101,7 @@ public Index( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual InternalIndexBuilder Builder + public virtual InternalIndexBuilder? Builder { [DebuggerStepThrough] get; [DebuggerStepThrough] @@ -187,9 +189,9 @@ private void UpdateIsUniqueConfigurationSource(ConfigurationSource configuration /// The annotation that was set. protected override IConventionAnnotation OnAnnotationSet( string name, - IConventionAnnotation annotation, - IConventionAnnotation oldAnnotation) - => Builder.ModelBuilder.Metadata.ConventionDispatcher.OnIndexAnnotationChanged(Builder, name, annotation, oldAnnotation); + IConventionAnnotation? annotation, + IConventionAnnotation? oldAnnotation) + => Builder!.ModelBuilder.Metadata.ConventionDispatcher.OnIndexAnnotationChanged(Builder, name, annotation, oldAnnotation); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -271,7 +273,7 @@ IMutableEntityType IMutableIndex.DeclaringEntityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionIndexBuilder IConventionIndex.Builder + IConventionIndexBuilder? IConventionIndex.Builder { [DebuggerStepThrough] get => Builder; } @@ -282,7 +284,7 @@ IConventionIndexBuilder IConventionIndex.Builder /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionAnnotatableBuilder IConventionAnnotatable.Builder + IConventionAnnotatableBuilder? IConventionAnnotatable.Builder { [DebuggerStepThrough] get => Builder; } diff --git a/src/EFCore/Metadata/Internal/IndexExtensions.cs b/src/EFCore/Metadata/Internal/IndexExtensions.cs index b2ccd259019..d459e9479e5 100644 --- a/src/EFCore/Metadata/Internal/IndexExtensions.cs +++ b/src/EFCore/Metadata/Internal/IndexExtensions.cs @@ -4,6 +4,8 @@ using System.Runtime.CompilerServices; using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// diff --git a/src/EFCore/Metadata/Internal/InternalPropertyBuilder.cs b/src/EFCore/Metadata/Internal/InternalPropertyBuilder.cs index ab445f0f990..7161fc37207 100644 --- a/src/EFCore/Metadata/Internal/InternalPropertyBuilder.cs +++ b/src/EFCore/Metadata/Internal/InternalPropertyBuilder.cs @@ -482,7 +482,7 @@ public virtual bool CanSetConversion([CanBeNull] Type providerClrType, Configura /// doing so can result in application failures when updating to a new Entity Framework Core release. /// public virtual InternalPropertyBuilder HasTypeMapping( - [CanBeNull] CoreTypeMapping typeMapping, + [NotNull] CoreTypeMapping typeMapping, ConfigurationSource configurationSource) { if (CanSetTypeMapping(typeMapping, configurationSource)) diff --git a/src/EFCore/Metadata/Internal/Key.cs b/src/EFCore/Metadata/Internal/Key.cs index 4e4dd69cbf3..3f39a4e5b67 100644 --- a/src/EFCore/Metadata/Internal/Key.cs +++ b/src/EFCore/Metadata/Internal/Key.cs @@ -13,6 +13,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -26,9 +28,9 @@ public class Key : ConventionAnnotatable, IMutableKey, IConventionKey private ConfigurationSource _configurationSource; // Warning: Never access these fields directly as access needs to be thread-safe - private Func _identityMapFactory; + private Func? _identityMapFactory; - private object _principalKeyValueFactory; + private object? _principalKeyValueFactory; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -72,7 +74,7 @@ public virtual EntityType DeclaringEntityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual InternalKeyBuilder Builder + public virtual InternalKeyBuilder? Builder { [DebuggerStepThrough] get; [DebuggerStepThrough] @@ -114,9 +116,9 @@ public virtual void UpdateConfigurationSource(ConfigurationSource configurationS /// The annotation that was set. protected override IConventionAnnotation OnAnnotationSet( string name, - IConventionAnnotation annotation, - IConventionAnnotation oldAnnotation) - => Builder.ModelBuilder.Metadata.ConventionDispatcher.OnKeyAnnotationChanged(Builder, name, annotation, oldAnnotation); + IConventionAnnotation? annotation, + IConventionAnnotation? oldAnnotation) + => Builder!.ModelBuilder.Metadata.ConventionDispatcher.OnKeyAnnotationChanged(Builder, name, annotation, oldAnnotation); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -153,7 +155,7 @@ public virtual IPrincipalKeyValueFactory GetPrincipalKeyValueFactory /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ISet ReferencingForeignKeys { get; [param: CanBeNull] set; } + public virtual ISet? ReferencingForeignKeys { get; [param: CanBeNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -225,7 +227,7 @@ IMutableEntityType IMutableKey.DeclaringEntityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionKeyBuilder IConventionKey.Builder + IConventionKeyBuilder? IConventionKey.Builder { [DebuggerStepThrough] get => Builder; } @@ -236,7 +238,7 @@ IConventionKeyBuilder IConventionKey.Builder /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionAnnotatableBuilder IConventionAnnotatable.Builder + IConventionAnnotatableBuilder? IConventionAnnotatable.Builder { [DebuggerStepThrough] get => Builder; diff --git a/src/EFCore/Metadata/Internal/KeyExtensions.cs b/src/EFCore/Metadata/Internal/KeyExtensions.cs index 940725216d9..8eb6f5d3b37 100644 --- a/src/EFCore/Metadata/Internal/KeyExtensions.cs +++ b/src/EFCore/Metadata/Internal/KeyExtensions.cs @@ -6,6 +6,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// diff --git a/src/EFCore/Metadata/Internal/Model.cs b/src/EFCore/Metadata/Internal/Model.cs index b5b330b47fa..3799710fe2a 100644 --- a/src/EFCore/Metadata/Internal/Model.cs +++ b/src/EFCore/Metadata/Internal/Model.cs @@ -17,6 +17,8 @@ using Microsoft.EntityFrameworkCore.Utilities; using Microsoft.Extensions.DependencyInjection; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -83,7 +85,7 @@ public Model() /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public Model([NotNull] ConventionSet conventions, [CanBeNull] ModelDependencies modelDependencies = null) + public Model([NotNull] ConventionSet conventions, [CanBeNull] ModelDependencies? modelDependencies = null) { ModelDependencies = modelDependencies; var dispatcher = new ConventionDispatcher(conventions); @@ -99,6 +101,7 @@ public Model([NotNull] ConventionSet conventions, [CanBeNull] ModelDependencies /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// + // Becomes null once the model becomes read only; after this point, should never get accessed. public virtual ConventionDispatcher ConventionDispatcher { get; private set; } /// @@ -107,7 +110,7 @@ public Model([NotNull] ConventionSet conventions, [CanBeNull] ModelDependencies /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ModelDependencies ModelDependencies { get; private set; } + public virtual ModelDependencies? ModelDependencies { get; private set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -149,7 +152,7 @@ public virtual IEnumerable GetEntityTypes() /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType AddEntityType( + public virtual EntityType? AddEntityType( [NotNull] string name, ConfigurationSource configurationSource) { @@ -166,7 +169,7 @@ public virtual EntityType AddEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType AddEntityType( + public virtual EntityType? AddEntityType( [NotNull] Type type, ConfigurationSource configurationSource) { @@ -183,7 +186,7 @@ public virtual EntityType AddEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType AddEntityType( + public virtual EntityType? AddEntityType( [NotNull] string name, [NotNull] Type type, ConfigurationSource configurationSource) @@ -196,7 +199,7 @@ public virtual EntityType AddEntityType( return AddEntityType(entityType); } - private EntityType AddEntityType(EntityType entityType) + private EntityType? AddEntityType(EntityType entityType) { var entityTypeName = entityType.Name; if (entityType.HasDefiningNavigation()) @@ -211,8 +214,9 @@ private EntityType AddEntityType(EntityType entityType) for (var i = 0; i < detachedEntityTypes.Count; i++) { var (definingNavigation, definingEntityType) = detachedEntityTypes[i]; + // TODO-NULLABLE: Put MemberNotNull on HasDefiningNavigation when we target net5.0 if (definingNavigation == entityType.DefiningNavigationName - && definingEntityType == entityType.DefiningEntityType.Name) + && definingEntityType == entityType.DefiningEntityType!.Name) { detachedEntityTypes.RemoveAt(i); break; @@ -251,6 +255,8 @@ private EntityType AddEntityType(EntityType entityType) CoreStrings.ClashingNonSharedType(entityType.Name, entityType.ClrType.DisplayName())); } + // TODO-NULLABLE: Put MemberNotNull on HasSharedClrType when we target net5.0 + Check.DebugAssert(entityType.ClrType != null, "entityType.ClrType is null"); if (_sharedTypes.TryGetValue(entityType.ClrType, out var existingConfigurationSource)) { _sharedTypes[entityType.ClrType] = entityType.GetConfigurationSource().Max(existingConfigurationSource); @@ -269,7 +275,7 @@ private EntityType AddEntityType(EntityType entityType) _entityTypes.Add(entityTypeName, entityType); } - return (EntityType)ConventionDispatcher.OnEntityTypeAdded(entityType.Builder)?.Metadata; + return (EntityType?)ConventionDispatcher.OnEntityTypeAdded(entityType.Builder)?.Metadata; } /// @@ -278,7 +284,7 @@ private EntityType AddEntityType(EntityType entityType) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType FindEntityType([NotNull] Type type) + public virtual EntityType? FindEntityType([NotNull] Type type) => FindEntityType(GetDisplayName(type)); /// @@ -287,7 +293,7 @@ public virtual EntityType FindEntityType([NotNull] Type type) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType FindEntityType([NotNull] string name) + public virtual EntityType? FindEntityType([NotNull] string name) { Check.DebugAssert(!string.IsNullOrEmpty(name), "name is null or empty"); return _entityTypes.TryGetValue(name, out var entityType) @@ -301,7 +307,7 @@ public virtual EntityType FindEntityType([NotNull] string name) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType RemoveEntityType([NotNull] Type type) + public virtual EntityType? RemoveEntityType([NotNull] Type type) => RemoveEntityType(FindEntityType(type)); /// @@ -310,7 +316,7 @@ public virtual EntityType RemoveEntityType([NotNull] Type type) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType RemoveEntityType([NotNull] string name) + public virtual EntityType? RemoveEntityType([NotNull] string name) => RemoveEntityType(FindEntityType(name)); private static void AssertCanRemove(EntityType entityType) @@ -351,7 +357,7 @@ private static void AssertCanRemove(EntityType entityType) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType RemoveEntityType([CanBeNull] EntityType entityType) + public virtual EntityType? RemoveEntityType([CanBeNull] EntityType? entityType) { if (entityType?.Builder == null) { @@ -393,7 +399,7 @@ public virtual EntityType RemoveEntityType([CanBeNull] EntityType entityType) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType AddEntityType( + public virtual EntityType? AddEntityType( [NotNull] string name, [NotNull] string definingNavigationName, [NotNull] EntityType definingEntityType, @@ -412,7 +418,7 @@ public virtual EntityType AddEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType AddEntityType( + public virtual EntityType? AddEntityType( [NotNull] Type type, [NotNull] string definingNavigationName, [NotNull] EntityType definingEntityType, @@ -496,8 +502,9 @@ public virtual bool HasOtherEntityTypesWithDefiningNavigation([NotNull] EntityTy if (_detachedEntityTypesWithDefiningNavigation.TryGetValue(entityType.Name, out var detachedEntityTypesWithSameType)) { + // TODO-NULLABLE: Put MemberNotNull on HasDefiningNavigation when we target net5.0 if (detachedEntityTypesWithSameType.Any( - e => e.Item1 != entityType.DefiningNavigationName || e.Item2 != entityType.DefiningEntityType.Name)) + e => e.Item1 != entityType.DefiningNavigationName || e.Item2 != entityType.DefiningEntityType!.Name)) { return true; } @@ -535,7 +542,7 @@ private bool EntityTypeShouldHaveDefiningNavigation(in TypeIdentity type) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType FindEntityType( + public virtual EntityType? FindEntityType( [NotNull] Type type, [NotNull] string definingNavigationName, [NotNull] EntityType definingEntityType) @@ -547,7 +554,7 @@ public virtual EntityType FindEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType FindEntityType( + public virtual EntityType? FindEntityType( [NotNull] string name, [NotNull] string definingNavigationName, [NotNull] EntityType definingEntityType) @@ -562,12 +569,13 @@ public virtual EntityType FindEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType FindActualEntityType([NotNull] EntityType entityType) + public virtual EntityType? FindActualEntityType([NotNull] EntityType entityType) + // TODO-NULLABLE: Put MemberNotNull on HasDefiningNavigation when we target net5.0 => entityType.Builder != null ? entityType : (entityType.HasDefiningNavigation() - ? FindActualEntityType(entityType.DefiningEntityType) - ?.FindNavigation(entityType.DefiningNavigationName)?.TargetEntityType + ? FindActualEntityType(entityType.DefiningEntityType!) + ?.FindNavigation(entityType.DefiningNavigationName!)?.TargetEntityType : FindEntityType(entityType.Name)); /// @@ -576,7 +584,7 @@ public virtual EntityType FindActualEntityType([NotNull] EntityType entityType) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Type FindClrType([NotNull] string name) + public virtual Type? FindClrType([NotNull] string name) => _entityTypes.TryGetValue(name, out var entityType) ? entityType.HasSharedClrType ? null @@ -621,7 +629,7 @@ public virtual IReadOnlyCollection GetEntityTypes([NotNull] string n /// public virtual IReadOnlyList FindLeastDerivedEntityTypes( [NotNull] Type type, - [CanBeNull] Func condition = null) + [CanBeNull] Func? condition = null) { var derivedLevels = new Dictionary { [type] = 0 }; @@ -647,7 +655,7 @@ public virtual IReadOnlyList FindLeastDerivedEntityTypes( return new List(); } - private static int GetDerivedLevel(Type derivedType, Dictionary derivedLevels) + private static int GetDerivedLevel(Type? derivedType, Dictionary derivedLevels) { if (derivedType?.BaseType == null) { @@ -672,7 +680,7 @@ private static int GetDerivedLevel(Type derivedType, Dictionary deriv /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType RemoveEntityType( + public virtual EntityType? RemoveEntityType( [NotNull] Type type, [NotNull] string definingNavigationName, [NotNull] EntityType definingEntityType) @@ -684,7 +692,7 @@ public virtual EntityType RemoveEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType RemoveEntityType( + public virtual EntityType? RemoveEntityType( [NotNull] string name, [NotNull] string definingNavigationName, [NotNull] EntityType definingEntityType) @@ -696,7 +704,7 @@ public virtual EntityType RemoveEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string AddIgnored( + public virtual string? AddIgnored( [NotNull] Type type, ConfigurationSource configurationSource) => AddIgnored(GetDisplayName(Check.NotNull(type, nameof(type))), type, configurationSource); @@ -707,7 +715,7 @@ public virtual string AddIgnored( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string AddIgnored( + public virtual string? AddIgnored( [NotNull] string name, ConfigurationSource configurationSource) => AddIgnored(Check.NotNull(name, nameof(name)), null, configurationSource); @@ -718,9 +726,9 @@ public virtual string AddIgnored( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string AddIgnored( + public virtual string? AddIgnored( [NotNull] string name, - [CanBeNull] Type type, + [CanBeNull] Type? type, ConfigurationSource configurationSource) { if (_ignoredTypeNames.TryGetValue(name, out var existingIgnoredConfigurationSource)) @@ -801,7 +809,7 @@ public virtual bool IsShared([NotNull] Type type) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string RemoveIgnored([NotNull] Type type) + public virtual string? RemoveIgnored([NotNull] Type type) { Check.NotNull(type, nameof(type)); return RemoveIgnored(GetDisplayName(type)); @@ -813,7 +821,7 @@ public virtual string RemoveIgnored([NotNull] Type type) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string RemoveIgnored(string name) + public virtual string? RemoveIgnored(string name) { Check.NotNull(name, nameof(name)); @@ -885,7 +893,7 @@ public virtual void AddOwned([NotNull] Type type, ConfigurationSource configurat /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string RemoveOwned([NotNull] Type type) + public virtual string? RemoveOwned([NotNull] Type type) { if (!(this[CoreAnnotationNames.OwnedTypes] is Dictionary ownedTypes)) { @@ -981,8 +989,8 @@ public virtual ChangeTrackingStrategy GetChangeTrackingStrategy() /// The annotation that was set. protected override IConventionAnnotation OnAnnotationSet( string name, - IConventionAnnotation annotation, - IConventionAnnotation oldAnnotation) + IConventionAnnotation? annotation, + IConventionAnnotation? oldAnnotation) => ConventionDispatcher.OnModelAnnotationChanged(Builder, name, annotation, oldAnnotation); /// @@ -994,13 +1002,13 @@ protected override IConventionAnnotation OnAnnotationSet( public virtual IModel FinalizeModel() { ConventionDispatcher.AssertNoScope(); - IModel finalizedModel = ConventionDispatcher.OnModelFinalizing(Builder)?.Metadata; + IModel? finalizedModel = ConventionDispatcher.OnModelFinalizing(Builder)?.Metadata; if (finalizedModel != null) { finalizedModel = ConventionDispatcher.OnModelFinalized(finalizedModel); } - return (finalizedModel as Model)?.MakeReadonly() ?? finalizedModel; + return (finalizedModel as Model)?.MakeReadonly() ?? finalizedModel!; } /// @@ -1011,7 +1019,8 @@ public virtual IModel FinalizeModel() /// private IModel MakeReadonly() { - ConventionDispatcher = null; + // ConventionDispatcher should never be accessed once the model is made read-only. + ConventionDispatcher = null!; ModelDependencies = null; IsValidated = true; return this; @@ -1057,7 +1066,7 @@ public virtual bool SkipDetectChanges /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual object RelationalModel + public virtual object? RelationalModel => this["Relational:RelationalModel"]; /// @@ -1077,7 +1086,7 @@ public virtual DebugView DebugView /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IEntityType IModel.FindEntityType(string name) + IEntityType? IModel.FindEntityType(string name) => FindEntityType(name); /// @@ -1095,7 +1104,7 @@ IEnumerable IModel.GetEntityTypes() /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IMutableEntityType IMutableModel.FindEntityType(string name) + IMutableEntityType? IMutableModel.FindEntityType(string name) => FindEntityType(name); /// @@ -1105,7 +1114,7 @@ IMutableEntityType IMutableModel.FindEntityType(string name) /// doing so can result in application failures when updating to a new Entity Framework Core release. /// IMutableEntityType IMutableModel.AddEntityType(string name) - => AddEntityType(name, ConfigurationSource.Explicit); + => AddEntityType(name, ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -1114,7 +1123,7 @@ IMutableEntityType IMutableModel.AddEntityType(string name) /// doing so can result in application failures when updating to a new Entity Framework Core release. /// IMutableEntityType IMutableModel.AddEntityType(Type type) - => AddEntityType(type, ConfigurationSource.Explicit); + => AddEntityType(type, ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -1123,7 +1132,7 @@ IMutableEntityType IMutableModel.AddEntityType(Type type) /// doing so can result in application failures when updating to a new Entity Framework Core release. /// IMutableEntityType IMutableModel.AddEntityType(string name, Type type) - => AddEntityType(name, type, ConfigurationSource.Explicit); + => AddEntityType(name, type, ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -1131,7 +1140,7 @@ IMutableEntityType IMutableModel.AddEntityType(string name, Type type) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IMutableEntityType IMutableModel.RemoveEntityType(IMutableEntityType entityType) + IMutableEntityType? IMutableModel.RemoveEntityType(IMutableEntityType entityType) => RemoveEntityType((EntityType)entityType); /// @@ -1140,7 +1149,7 @@ IMutableEntityType IMutableModel.RemoveEntityType(IMutableEntityType entityType) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IEntityType IModel.FindEntityType(string name, string definingNavigationName, IEntityType definingEntityType) + IEntityType? IModel.FindEntityType(string name, string definingNavigationName, IEntityType definingEntityType) => FindEntityType(name, definingNavigationName, (EntityType)definingEntityType); /// @@ -1149,7 +1158,7 @@ IEntityType IModel.FindEntityType(string name, string definingNavigationName, IE /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IMutableEntityType IMutableModel.FindEntityType( + IMutableEntityType? IMutableModel.FindEntityType( string name, string definingNavigationName, IMutableEntityType definingEntityType) @@ -1165,7 +1174,7 @@ IMutableEntityType IMutableModel.AddEntityType( string name, string definingNavigationName, IMutableEntityType definingEntityType) - => AddEntityType(name, definingNavigationName, (EntityType)definingEntityType, ConfigurationSource.Explicit); + => AddEntityType(name, definingNavigationName, (EntityType)definingEntityType, ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -1177,7 +1186,7 @@ IMutableEntityType IMutableModel.AddEntityType( Type type, string definingNavigationName, IMutableEntityType definingEntityType) - => AddEntityType(type, definingNavigationName, (EntityType)definingEntityType, ConfigurationSource.Explicit); + => AddEntityType(type, definingNavigationName, (EntityType)definingEntityType, ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -1195,7 +1204,7 @@ IEnumerable IMutableModel.GetEntityTypes() /// doing so can result in application failures when updating to a new Entity Framework Core release. /// string IMutableModel.AddIgnored(string name) - => AddIgnored(name, ConfigurationSource.Explicit); + => AddIgnored(name, ConfigurationSource.Explicit)!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -1227,7 +1236,7 @@ IConventionAnnotatableBuilder IConventionAnnotatable.Builder /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionEntityType IConventionModel.FindEntityType(string name) + IConventionEntityType? IConventionModel.FindEntityType(string name) => FindEntityType(name); /// @@ -1236,7 +1245,7 @@ IConventionEntityType IConventionModel.FindEntityType(string name) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionEntityType IConventionModel.FindEntityType( + IConventionEntityType? IConventionModel.FindEntityType( string name, string definingNavigationName, IConventionEntityType definingEntityType) @@ -1248,7 +1257,7 @@ IConventionEntityType IConventionModel.FindEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionEntityType IConventionModel.AddEntityType(string name, bool fromDataAnnotation) + IConventionEntityType? IConventionModel.AddEntityType(string name, bool fromDataAnnotation) => AddEntityType(name, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// @@ -1257,7 +1266,7 @@ IConventionEntityType IConventionModel.AddEntityType(string name, bool fromDataA /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionEntityType IConventionModel.AddEntityType(Type type, bool fromDataAnnotation) + IConventionEntityType? IConventionModel.AddEntityType(Type type, bool fromDataAnnotation) => AddEntityType(type, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// @@ -1266,7 +1275,7 @@ IConventionEntityType IConventionModel.AddEntityType(Type type, bool fromDataAnn /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionEntityType IConventionModel.AddEntityType(string name, Type type, bool fromDataAnnotation) + IConventionEntityType? IConventionModel.AddEntityType(string name, Type type, bool fromDataAnnotation) => AddEntityType(name, type, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// @@ -1275,7 +1284,7 @@ IConventionEntityType IConventionModel.AddEntityType(string name, Type type, boo /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionEntityType IConventionModel.AddEntityType( + IConventionEntityType? IConventionModel.AddEntityType( string name, string definingNavigationName, IConventionEntityType definingEntityType, @@ -1290,7 +1299,7 @@ IConventionEntityType IConventionModel.AddEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionEntityType IConventionModel.AddEntityType( + IConventionEntityType? IConventionModel.AddEntityType( Type type, string definingNavigationName, IConventionEntityType definingEntityType, @@ -1305,7 +1314,7 @@ IConventionEntityType IConventionModel.AddEntityType( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionEntityType IConventionModel.RemoveEntityType(IConventionEntityType entityType) + IConventionEntityType? IConventionModel.RemoveEntityType(IConventionEntityType entityType) => RemoveEntityType((EntityType)entityType); /// @@ -1323,7 +1332,7 @@ IEnumerable IConventionModel.GetEntityTypes() /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - string IConventionModel.AddIgnored(string name, bool fromDataAnnotation) + string? IConventionModel.AddIgnored(string name, bool fromDataAnnotation) => AddIgnored(name, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// diff --git a/src/EFCore/Metadata/Internal/ModelExtensions.cs b/src/EFCore/Metadata/Internal/ModelExtensions.cs index 775566b40b5..7f83658c3b9 100644 --- a/src/EFCore/Metadata/Internal/ModelExtensions.cs +++ b/src/EFCore/Metadata/Internal/ModelExtensions.cs @@ -6,6 +6,8 @@ using System.Runtime.CompilerServices; using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// diff --git a/src/EFCore/Metadata/Internal/MutableEntityTypeExtensions.cs b/src/EFCore/Metadata/Internal/MutableEntityTypeExtensions.cs index 0cea73808bd..f348fd3850e 100644 --- a/src/EFCore/Metadata/Internal/MutableEntityTypeExtensions.cs +++ b/src/EFCore/Metadata/Internal/MutableEntityTypeExtensions.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// diff --git a/src/EFCore/Metadata/Internal/Navigation.cs b/src/EFCore/Metadata/Internal/Navigation.cs index f141655440d..5fad9adfc7f 100644 --- a/src/EFCore/Metadata/Internal/Navigation.cs +++ b/src/EFCore/Metadata/Internal/Navigation.cs @@ -11,6 +11,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -22,7 +24,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal public class Navigation : PropertyBase, IMutableNavigation, IConventionNavigation { // Warning: Never access these fields directly as access needs to be thread-safe - private IClrCollectionAccessor _collectionAccessor; + private IClrCollectionAccessor? _collectionAccessor; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -32,8 +34,8 @@ public class Navigation : PropertyBase, IMutableNavigation, IConventionNavigatio /// public Navigation( [NotNull] string name, - [CanBeNull] PropertyInfo propertyInfo, - [CanBeNull] FieldInfo fieldInfo, + [CanBeNull] PropertyInfo? propertyInfo, + [CanBeNull] FieldInfo? fieldInfo, [NotNull] ForeignKey foreignKey) : base(name, propertyInfo, fieldInfo, ConfigurationSource.Convention) { @@ -67,7 +69,7 @@ public override Type ClrType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual InternalNavigationBuilder Builder { get; [param: CanBeNull] set; } + public virtual InternalNavigationBuilder? Builder { get; [param: CanBeNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -126,7 +128,7 @@ public virtual bool IsOnDependent public override ConfigurationSource GetConfigurationSource() => (ConfigurationSource)(IsOnDependent ? ForeignKey.GetDependentToPrincipalConfigurationSource() - : ForeignKey.GetPrincipalToDependentConfigurationSource()); + : ForeignKey.GetPrincipalToDependentConfigurationSource())!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -163,12 +165,23 @@ public virtual void SetIsEagerLoaded(bool? eagerLoaded, ConfigurationSource conf /// public static bool IsCompatible( [NotNull] string navigationName, - [CanBeNull] MemberInfo navigationProperty, + [CanBeNull] MemberInfo? navigationProperty, [NotNull] EntityType sourceType, [NotNull] EntityType targetType, bool? shouldBeCollection, bool shouldThrow) { + var sourceClrType = sourceType.ClrType; + if (sourceClrType == null) + { + if (shouldThrow) + { + throw new InvalidOperationException(CoreStrings.NavigationFromShadowEntity(navigationName, sourceType.DisplayName())); + } + + return false; + } + var targetClrType = targetType.ClrType; if (targetClrType == null) { @@ -182,7 +195,7 @@ public static bool IsCompatible( } return navigationProperty == null - || IsCompatible(navigationProperty, sourceType.ClrType, targetClrType, shouldBeCollection, shouldThrow); + || IsCompatible(navigationProperty, sourceClrType, targetClrType, shouldBeCollection, shouldThrow); } /// @@ -265,7 +278,7 @@ public virtual Navigation Inverse /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Navigation SetInverse([CanBeNull] string inverseName, ConfigurationSource configurationSource) + public virtual Navigation? SetInverse([CanBeNull] string? inverseName, ConfigurationSource configurationSource) => IsOnDependent ? ForeignKey.HasPrincipalToDependent(inverseName, configurationSource) : ForeignKey.SetDependentToPrincipal(inverseName, configurationSource); @@ -276,7 +289,7 @@ public virtual Navigation SetInverse([CanBeNull] string inverseName, Configurati /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Navigation SetInverse([CanBeNull] MemberInfo inverse, ConfigurationSource configurationSource) + public virtual Navigation? SetInverse([CanBeNull] MemberInfo? inverse, ConfigurationSource configurationSource) => IsOnDependent ? ForeignKey.HasPrincipalToDependent(inverse, configurationSource) : ForeignKey.SetDependentToPrincipal(inverse, configurationSource); @@ -311,8 +324,8 @@ public virtual IClrCollectionAccessor CollectionAccessor /// The annotation that was set. protected override IConventionAnnotation OnAnnotationSet( string name, - IConventionAnnotation annotation, - IConventionAnnotation oldAnnotation) + IConventionAnnotation? annotation, + IConventionAnnotation? oldAnnotation) => DeclaringType.Model.ConventionDispatcher.OnNavigationAnnotationChanged( ForeignKey.Builder, this, name, annotation, oldAnnotation); @@ -348,22 +361,22 @@ IForeignKey INavigation.ForeignKey } [DebuggerStepThrough] - IMutableNavigation IMutableNavigation.SetInverse([CanBeNull] string inverseName) + IMutableNavigation? IMutableNavigation.SetInverse(string? inverseName) => SetInverse(inverseName, ConfigurationSource.Explicit); [DebuggerStepThrough] - IMutableNavigation IMutableNavigation.SetInverse([CanBeNull] MemberInfo inverse) + IMutableNavigation? IMutableNavigation.SetInverse(MemberInfo? inverse) => SetInverse(inverse, ConfigurationSource.Explicit); [DebuggerStepThrough] - IConventionNavigation IConventionNavigation.SetInverse([CanBeNull] string inverseName, bool fromDataAnnotation) + IConventionNavigation? IConventionNavigation.SetInverse(string? inverseName, bool fromDataAnnotation) => SetInverse(inverseName, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); [DebuggerStepThrough] - IConventionNavigation IConventionNavigation.SetInverse([CanBeNull] MemberInfo inverse, bool fromDataAnnotation) + IConventionNavigation? IConventionNavigation.SetInverse(MemberInfo? inverse, bool fromDataAnnotation) => SetInverse(inverse, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); - IConventionNavigationBuilder IConventionNavigation.Builder + IConventionNavigationBuilder? IConventionNavigation.Builder { [DebuggerStepThrough] get => Builder; } @@ -374,7 +387,7 @@ IConventionNavigationBuilder IConventionNavigation.Builder /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionAnnotatableBuilder IConventionAnnotatable.Builder + IConventionAnnotatableBuilder? IConventionAnnotatable.Builder { [DebuggerStepThrough] get => Builder; } diff --git a/src/EFCore/Metadata/Internal/NavigationExtensions.cs b/src/EFCore/Metadata/Internal/NavigationExtensions.cs index 36f38058eae..3752d30a5e4 100644 --- a/src/EFCore/Metadata/Internal/NavigationExtensions.cs +++ b/src/EFCore/Metadata/Internal/NavigationExtensions.cs @@ -5,6 +5,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Internal; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -21,7 +23,7 @@ public static class NavigationExtensions /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static MemberIdentity CreateMemberIdentity([CanBeNull] this INavigation navigation) + public static MemberIdentity CreateMemberIdentity([CanBeNull] this INavigation? navigation) => navigation?.GetIdentifyingMemberInfo() == null ? MemberIdentity.Create(navigation?.Name) : MemberIdentity.Create(navigation.GetIdentifyingMemberInfo()); diff --git a/src/EFCore/Metadata/Internal/Property.cs b/src/EFCore/Metadata/Internal/Property.cs index 1737966d0a7..fef8e8a734b 100644 --- a/src/EFCore/Metadata/Internal/Property.cs +++ b/src/EFCore/Metadata/Internal/Property.cs @@ -15,6 +15,9 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.Utilities; using Microsoft.EntityFrameworkCore.ValueGeneration; +using CA = System.Diagnostics.CodeAnalysis; + +#nullable enable namespace Microsoft.EntityFrameworkCore.Metadata.Internal { @@ -29,7 +32,7 @@ public class Property : PropertyBase, IMutableProperty, IConventionProperty private bool? _isConcurrencyToken; private bool? _isNullable; private ValueGenerated? _valueGenerated; - private CoreTypeMapping _typeMapping; + private CoreTypeMapping? _typeMapping; private ConfigurationSource? _typeConfigurationSource; private ConfigurationSource? _isNullableConfigurationSource; @@ -46,8 +49,8 @@ public class Property : PropertyBase, IMutableProperty, IConventionProperty public Property( [NotNull] string name, [NotNull] Type clrType, - [CanBeNull] PropertyInfo propertyInfo, - [CanBeNull] FieldInfo fieldInfo, + [CanBeNull] PropertyInfo? propertyInfo, + [CanBeNull] FieldInfo? fieldInfo, [NotNull] EntityType declaringEntityType, ConfigurationSource configurationSource, ConfigurationSource? typeConfigurationSource) @@ -97,7 +100,7 @@ public override TypeBase DeclaringType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual InternalPropertyBuilder Builder { get; [param: CanBeNull] set; } + public virtual InternalPropertyBuilder? Builder { get; [param: CanBeNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -194,7 +197,7 @@ private bool DefaultIsNullable /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - protected override FieldInfo OnFieldInfoSet(FieldInfo newFieldInfo, FieldInfo oldFieldInfo) + protected override FieldInfo? OnFieldInfoSet(FieldInfo? newFieldInfo, FieldInfo? oldFieldInfo) => DeclaringEntityType.Model.ConventionDispatcher.OnPropertyFieldChanged(Builder, newFieldInfo, oldFieldInfo); /// @@ -395,7 +398,7 @@ private static bool DefaultIsConcurrencyToken /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string CheckAfterSaveBehavior(PropertySaveBehavior behavior) + public virtual string? CheckAfterSaveBehavior(PropertySaveBehavior behavior) => behavior != PropertySaveBehavior.Throw && this.IsKey() ? CoreStrings.KeyPropertyMustBeReadOnly(Name, DeclaringEntityType.DisplayName()) @@ -407,8 +410,8 @@ public virtual string CheckAfterSaveBehavior(PropertySaveBehavior behavior) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Func SetValueGeneratorFactory( - [CanBeNull] Func factory, + public virtual Func? SetValueGeneratorFactory( + [CanBeNull] Func? factory, ConfigurationSource configurationSource) { SetAnnotation(CoreAnnotationNames.ValueGeneratorFactory, factory, configurationSource); @@ -422,8 +425,8 @@ public virtual Func SetValueGeneratorFac /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ValueConverter SetValueConverter( - [CanBeNull] ValueConverter converter, + public virtual ValueConverter? SetValueConverter( + [CanBeNull] ValueConverter? converter, ConfigurationSource configurationSource) { var errorString = CheckValueConverter(converter); @@ -443,7 +446,7 @@ public virtual ValueConverter SetValueConverter( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string CheckValueConverter([CanBeNull] ValueConverter converter) + public virtual string? CheckValueConverter([CanBeNull] ValueConverter? converter) => converter != null && converter.ModelClrType.UnwrapNullableType() != ClrType.UnwrapNullableType() ? CoreStrings.ConverterPropertyMismatch( @@ -459,7 +462,7 @@ public virtual string CheckValueConverter([CanBeNull] ValueConverter converter) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual Type SetProviderClrType([CanBeNull] Type providerClrType, ConfigurationSource configurationSource) + public virtual Type? SetProviderClrType([CanBeNull] Type? providerClrType, ConfigurationSource configurationSource) { this.SetOrRemoveAnnotation(CoreAnnotationNames.ProviderClrType, providerClrType, configurationSource); @@ -472,7 +475,8 @@ public virtual Type SetProviderClrType([CanBeNull] Type providerClrType, Configu /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual CoreTypeMapping TypeMapping + [CA.DisallowNull] + public virtual CoreTypeMapping? TypeMapping { get => _typeMapping; [param: NotNull] @@ -488,9 +492,7 @@ public virtual CoreTypeMapping TypeMapping public virtual CoreTypeMapping SetTypeMapping([NotNull] CoreTypeMapping typeMapping, ConfigurationSource configurationSource) { _typeMapping = typeMapping; - _typeMappingConfigurationSource = typeMapping == null - ? (ConfigurationSource?)null - : configurationSource.Max(_typeMappingConfigurationSource); + _typeMappingConfigurationSource = configurationSource.Max(_typeMappingConfigurationSource); return typeMapping; } @@ -510,7 +512,7 @@ public virtual CoreTypeMapping SetTypeMapping([NotNull] CoreTypeMapping typeMapp /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ValueComparer SetValueComparer([CanBeNull] ValueComparer comparer, ConfigurationSource configurationSource) + public virtual ValueComparer? SetValueComparer([CanBeNull] ValueComparer? comparer, ConfigurationSource configurationSource) { var errorString = CheckValueComparer(comparer); if (errorString != null) @@ -529,7 +531,7 @@ public virtual ValueComparer SetValueComparer([CanBeNull] ValueComparer comparer /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual string CheckValueComparer([CanBeNull] ValueComparer comparer) + public virtual string? CheckValueComparer([CanBeNull] ValueComparer? comparer) => comparer != null && comparer.Type != ClrType ? CoreStrings.ComparerPropertyMismatch( @@ -545,7 +547,7 @@ public virtual string CheckValueComparer([CanBeNull] ValueComparer comparer) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual IKey PrimaryKey { get; [param: CanBeNull] set; } + public virtual IKey? PrimaryKey { get; [param: CanBeNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -553,7 +555,7 @@ public virtual string CheckValueComparer([CanBeNull] ValueComparer comparer) /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual List Keys { get; [param: CanBeNull] set; } + public virtual List? Keys { get; [param: CanBeNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -570,7 +572,7 @@ public virtual IEnumerable GetContainingKeys() /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual List ForeignKeys { get; [param: CanBeNull] set; } + public virtual List? ForeignKeys { get; [param: CanBeNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -587,7 +589,7 @@ public virtual IEnumerable GetContainingForeignKeys() /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual List Indexes { get; [param: CanBeNull] set; } + public virtual List? Indexes { get; [param: CanBeNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -605,10 +607,10 @@ public virtual IEnumerable GetContainingIndexes() /// The annotation set. /// The old annotation. /// The annotation that was set. - protected override IConventionAnnotation OnAnnotationSet( + protected override IConventionAnnotation? OnAnnotationSet( string name, - IConventionAnnotation annotation, - IConventionAnnotation oldAnnotation) + IConventionAnnotation? annotation, + IConventionAnnotation? oldAnnotation) => DeclaringType.Model.ConventionDispatcher.OnPropertyAnnotationChanged(Builder, name, annotation, oldAnnotation); /// @@ -640,9 +642,9 @@ public static bool AreCompatible([NotNull] IReadOnlyList properties, [ property.IsShadowProperty() || (entityType.HasClrType() && ((property.PropertyInfo != null - && entityType.GetRuntimeProperties().ContainsKey(property.Name)) + && entityType.GetRuntimeProperties()!.ContainsKey(property.Name)) || (property.FieldInfo != null - && entityType.GetRuntimeFields().ContainsKey(property.Name))))); + && entityType.GetRuntimeFields()!.ContainsKey(property.Name))))); } /// @@ -671,7 +673,7 @@ public virtual DebugView DebugView /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionPropertyBuilder IConventionProperty.Builder + IConventionPropertyBuilder? IConventionProperty.Builder { [DebuggerStepThrough] get => Builder; } @@ -682,7 +684,7 @@ IConventionPropertyBuilder IConventionProperty.Builder /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionAnnotatableBuilder IConventionAnnotatable.Builder + IConventionAnnotatableBuilder? IConventionAnnotatable.Builder { [DebuggerStepThrough] get => Builder; diff --git a/src/EFCore/Metadata/Internal/PropertyBase.cs b/src/EFCore/Metadata/Internal/PropertyBase.cs index d108560a524..7ed4b1e0842 100644 --- a/src/EFCore/Metadata/Internal/PropertyBase.cs +++ b/src/EFCore/Metadata/Internal/PropertyBase.cs @@ -13,6 +13,9 @@ using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Update; using Microsoft.EntityFrameworkCore.Utilities; +using CA = System.Diagnostics.CodeAnalysis; + +#nullable enable namespace Microsoft.EntityFrameworkCore.Metadata.Internal { @@ -24,17 +27,17 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal /// public abstract class PropertyBase : ConventionAnnotatable, IMutablePropertyBase, IConventionPropertyBase { - private FieldInfo _fieldInfo; + private FieldInfo? _fieldInfo; + private ConfigurationSource _configurationSource; private ConfigurationSource? _fieldInfoConfigurationSource; // Warning: Never access these fields directly as access needs to be thread-safe - private IClrPropertyGetter _getter; - private IClrPropertySetter _setter; - private IClrPropertySetter _materializationSetter; - private PropertyAccessors _accessors; - private PropertyIndexes _indexes; - private ConfigurationSource _configurationSource; - private IComparer _currentValueComparer; + private IClrPropertyGetter? _getter; + private IClrPropertySetter? _setter; + private IClrPropertySetter? _materializationSetter; + private PropertyAccessors? _accessors; + private PropertyIndexes? _indexes; + private IComparer? _currentValueComparer; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -44,8 +47,8 @@ public abstract class PropertyBase : ConventionAnnotatable, IMutablePropertyBase /// protected PropertyBase( [NotNull] string name, - [CanBeNull] PropertyInfo propertyInfo, - [CanBeNull] FieldInfo fieldInfo, + [CanBeNull] PropertyInfo? propertyInfo, + [CanBeNull] FieldInfo? fieldInfo, ConfigurationSource configurationSource) { Check.NotEmpty(name, nameof(name)); @@ -78,7 +81,7 @@ protected PropertyBase( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual PropertyInfo PropertyInfo { [DebuggerStepThrough] get; } + public virtual PropertyInfo? PropertyInfo { [DebuggerStepThrough] get; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -86,7 +89,7 @@ protected PropertyBase( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual FieldInfo FieldInfo + public virtual FieldInfo? FieldInfo { [DebuggerStepThrough] get => _fieldInfo; [DebuggerStepThrough] set => SetFieldInfo(value, ConfigurationSource.Explicit); @@ -129,7 +132,7 @@ public virtual void SetConfigurationSource(ConfigurationSource configurationSour /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual FieldInfo SetField([CanBeNull] string fieldName, ConfigurationSource configurationSource) + public virtual FieldInfo? SetField([CanBeNull] string? fieldName, ConfigurationSource configurationSource) { if (fieldName == null) { @@ -159,7 +162,7 @@ public static FieldInfo GetFieldInfo( { Check.DebugAssert(propertyName != null || !shouldThrow, "propertyName is null"); - if (!type.GetRuntimeFields().TryGetValue(fieldName, out var fieldInfo) + if (!type.GetRuntimeFields()!.TryGetValue(fieldName, out var fieldInfo) && shouldThrow) { throw new InvalidOperationException( @@ -175,7 +178,7 @@ public static FieldInfo GetFieldInfo( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual FieldInfo SetFieldInfo([CanBeNull] FieldInfo fieldInfo, ConfigurationSource configurationSource) + public virtual FieldInfo? SetFieldInfo([CanBeNull] FieldInfo? fieldInfo, ConfigurationSource configurationSource) { if (Equals(FieldInfo, fieldInfo)) { @@ -241,9 +244,9 @@ public virtual FieldInfo SetFieldInfo([CanBeNull] FieldInfo fieldInfo, Configura /// public static bool IsCompatible( [NotNull] FieldInfo fieldInfo, - [CanBeNull] Type propertyType, - [CanBeNull] Type entityType, - [CanBeNull] string propertyName, + [CanBeNull] Type? propertyType, + [CanBeNull] Type? entityType, + [CanBeNull] string? propertyName, bool shouldThrow) { Check.DebugAssert(propertyName != null || !shouldThrow, "propertyName is null"); @@ -287,6 +290,7 @@ public static bool IsCompatible( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// + [CA.AllowNull] public virtual PropertyIndexes PropertyIndexes { get @@ -319,7 +323,7 @@ public virtual PropertyIndexes PropertyIndexes /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - protected virtual FieldInfo OnFieldInfoSet([CanBeNull] FieldInfo newFieldInfo, [CanBeNull] FieldInfo oldFieldInfo) + protected virtual FieldInfo? OnFieldInfoSet([CanBeNull] FieldInfo? newFieldInfo, [CanBeNull] FieldInfo? oldFieldInfo) => newFieldInfo; /// @@ -465,7 +469,7 @@ IConventionTypeBase IConventionPropertyBase.DeclaringType /// doing so can result in application failures when updating to a new Entity Framework Core release. /// [DebuggerStepThrough] - FieldInfo IConventionPropertyBase.SetFieldInfo(FieldInfo fieldInfo, bool fromDataAnnotation) + FieldInfo? IConventionPropertyBase.SetFieldInfo(FieldInfo fieldInfo, bool fromDataAnnotation) => SetFieldInfo(fieldInfo, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); } } diff --git a/src/EFCore/Metadata/Internal/PropertyExtensions.cs b/src/EFCore/Metadata/Internal/PropertyExtensions.cs index f06ef1be478..d5f2ff28cf5 100644 --- a/src/EFCore/Metadata/Internal/PropertyExtensions.cs +++ b/src/EFCore/Metadata/Internal/PropertyExtensions.cs @@ -7,6 +7,8 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -59,7 +61,7 @@ public static IEnumerable GetReferencingForeignKeys([NotNull] this /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static IProperty GetGenerationProperty([NotNull] this IProperty property) + public static IProperty? FindGenerationProperty([NotNull] this IProperty property) { var traversalList = new List { property }; @@ -144,7 +146,7 @@ public static bool MayBeStoreGenerated([NotNull] this IProperty property) if (property.IsKey() || property.IsForeignKey()) { - var generationProperty = property.GetGenerationProperty(); + var generationProperty = property.FindGenerationProperty(); return (generationProperty != null) && (generationProperty.ValueGenerated != ValueGenerated.Never); } diff --git a/src/EFCore/Metadata/Internal/ServiceProperty.cs b/src/EFCore/Metadata/Internal/ServiceProperty.cs index 477452320dd..4bb914007aa 100644 --- a/src/EFCore/Metadata/Internal/ServiceProperty.cs +++ b/src/EFCore/Metadata/Internal/ServiceProperty.cs @@ -9,6 +9,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -19,7 +21,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal /// public class ServiceProperty : PropertyBase, IMutableServiceProperty, IConventionServiceProperty { - private ServiceParameterBinding _parameterBinding; + private ServiceParameterBinding? _parameterBinding; private ConfigurationSource? _parameterBindingConfigurationSource; @@ -31,8 +33,8 @@ public class ServiceProperty : PropertyBase, IMutableServiceProperty, IConventio /// public ServiceProperty( [NotNull] string name, - [CanBeNull] PropertyInfo propertyInfo, - [CanBeNull] FieldInfo fieldInfo, + [CanBeNull] PropertyInfo? propertyInfo, + [CanBeNull] FieldInfo? fieldInfo, [NotNull] EntityType declaringEntityType, ConfigurationSource configurationSource) : base(name, propertyInfo, fieldInfo, configurationSource) @@ -40,7 +42,7 @@ public ServiceProperty( Check.NotNull(declaringEntityType, nameof(declaringEntityType)); DeclaringEntityType = declaringEntityType; - ClrType = propertyInfo?.PropertyType ?? fieldInfo?.FieldType; + ClrType = (propertyInfo?.PropertyType ?? fieldInfo?.FieldType)!; Builder = new InternalServicePropertyBuilder(this, declaringEntityType.Model.Builder); } @@ -78,7 +80,7 @@ public override TypeBase DeclaringType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual InternalServicePropertyBuilder Builder + public virtual InternalServicePropertyBuilder? Builder { get; [param: CanBeNull] set; @@ -90,7 +92,7 @@ public virtual InternalServicePropertyBuilder Builder /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ServiceParameterBinding ParameterBinding + public virtual ServiceParameterBinding? ParameterBinding { get => _parameterBinding; set => SetParameterBinding(value, ConfigurationSource.Explicit); @@ -102,8 +104,8 @@ public virtual ServiceParameterBinding ParameterBinding /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ServiceParameterBinding SetParameterBinding( - [CanBeNull] ServiceParameterBinding parameterBinding, + public virtual ServiceParameterBinding? SetParameterBinding( + [CanBeNull] ServiceParameterBinding? parameterBinding, ConfigurationSource configurationSource) { _parameterBinding = parameterBinding; @@ -119,7 +121,7 @@ public virtual ServiceParameterBinding SetParameterBinding( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - ServiceParameterBinding IConventionServiceProperty.SetParameterBinding( + ServiceParameterBinding? IConventionServiceProperty.SetParameterBinding( ServiceParameterBinding parameterBinding, bool fromDataAnnotation) => SetParameterBinding( @@ -165,7 +167,7 @@ IMutableEntityType IMutableServiceProperty.DeclaringEntityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionServicePropertyBuilder IConventionServiceProperty.Builder + IConventionServicePropertyBuilder? IConventionServiceProperty.Builder { [DebuggerStepThrough] get => Builder; } @@ -176,7 +178,7 @@ IConventionServicePropertyBuilder IConventionServiceProperty.Builder /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - IConventionAnnotatableBuilder IConventionAnnotatable.Builder + IConventionAnnotatableBuilder? IConventionAnnotatable.Builder { [DebuggerStepThrough] get => Builder; } diff --git a/src/EFCore/Metadata/Internal/ServicePropertyExtensions.cs b/src/EFCore/Metadata/Internal/ServicePropertyExtensions.cs index 89855cf17d6..f0d2b4889a5 100644 --- a/src/EFCore/Metadata/Internal/ServicePropertyExtensions.cs +++ b/src/EFCore/Metadata/Internal/ServicePropertyExtensions.cs @@ -4,6 +4,8 @@ using System.Runtime.CompilerServices; using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -20,7 +22,7 @@ public static class ServicePropertyExtensions /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static ServiceParameterBinding GetParameterBinding([NotNull] this IServiceProperty serviceProperty) + public static ServiceParameterBinding? GetParameterBinding([NotNull] this IServiceProperty serviceProperty) => serviceProperty.AsServiceProperty().ParameterBinding; /// diff --git a/src/EFCore/Metadata/Internal/SkipNavigation.cs b/src/EFCore/Metadata/Internal/SkipNavigation.cs index ee1cb2068ac..771bdd99640 100644 --- a/src/EFCore/Metadata/Internal/SkipNavigation.cs +++ b/src/EFCore/Metadata/Internal/SkipNavigation.cs @@ -12,6 +12,8 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Utilities; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -26,8 +28,8 @@ public class SkipNavigation : PropertyBase, IMutableSkipNavigation, IConventionS private ConfigurationSource? _inverseConfigurationSource; // Warning: Never access these fields directly as access needs to be thread-safe - private IClrCollectionAccessor _collectionAccessor; - private ICollectionLoader _manyToManyLoader; + private IClrCollectionAccessor? _collectionAccessor; + private ICollectionLoader? _manyToManyLoader; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -37,8 +39,8 @@ public class SkipNavigation : PropertyBase, IMutableSkipNavigation, IConventionS /// public SkipNavigation( [NotNull] string name, - [CanBeNull] PropertyInfo propertyInfo, - [CanBeNull] FieldInfo fieldInfo, + [CanBeNull] PropertyInfo? propertyInfo, + [CanBeNull] FieldInfo? fieldInfo, [NotNull] EntityType declaringEntityType, [NotNull] EntityType targetEntityType, bool collection, @@ -77,7 +79,7 @@ private void ProcessForeignKey(ForeignKey foreignKey) /// doing so can result in application failures when updating to a new Entity Framework Core release. /// public override Type ClrType - => this.GetIdentifyingMemberInfo()?.GetMemberType(); + => this.GetIdentifyingMemberInfo().GetMemberType(); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -85,7 +87,7 @@ public override Type ClrType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual InternalSkipNavigationBuilder Builder { get; [param: CanBeNull] set; } + public virtual InternalSkipNavigationBuilder? Builder { get; [param: CanBeNull] set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -118,7 +120,7 @@ public override TypeBase DeclaringType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual EntityType JoinEntityType + public virtual EntityType? JoinEntityType => IsOnDependent ? ForeignKey?.PrincipalEntityType : ForeignKey?.DeclaringEntityType; /// @@ -127,7 +129,7 @@ public virtual EntityType JoinEntityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ForeignKey ForeignKey { get; [param: CanBeNull] private set; } + public virtual ForeignKey? ForeignKey { get; [param: CanBeNull] private set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -135,7 +137,7 @@ public virtual EntityType JoinEntityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual SkipNavigation Inverse { get; [param: CanBeNull] private set; } + public virtual SkipNavigation? Inverse { get; [param: CanBeNull] private set; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -159,15 +161,12 @@ public virtual EntityType JoinEntityType /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ForeignKey SetForeignKey([CanBeNull] ForeignKey foreignKey, ConfigurationSource configurationSource) + public virtual ForeignKey? SetForeignKey([CanBeNull] ForeignKey? foreignKey, ConfigurationSource configurationSource) { var oldForeignKey = ForeignKey; var isChanging = foreignKey != ForeignKey; - if (oldForeignKey != null) - { - oldForeignKey.ReferencingSkipNavigations.Remove(this); - } + oldForeignKey?.ReferencingSkipNavigations!.Remove(this); if (foreignKey == null) { @@ -175,7 +174,7 @@ public virtual ForeignKey SetForeignKey([CanBeNull] ForeignKey foreignKey, Confi _foreignKeyConfigurationSource = null; return isChanging - ? (ForeignKey)DeclaringEntityType.Model.ConventionDispatcher + ? (ForeignKey?)DeclaringEntityType.Model.ConventionDispatcher .OnSkipNavigationForeignKeyChanged(Builder, foreignKey, oldForeignKey) : foreignKey; } @@ -200,7 +199,7 @@ public virtual ForeignKey SetForeignKey([CanBeNull] ForeignKey foreignKey, Confi throw new InvalidOperationException( CoreStrings.SkipInverseMismatchedForeignKey( foreignKey.Properties.Format(), - Name, JoinEntityType.DisplayName(), + Name, JoinEntityType!.DisplayName(), Inverse.Name, Inverse.JoinEntityType.DisplayName())); } @@ -234,7 +233,7 @@ public virtual void UpdateForeignKeyConfigurationSource(ConfigurationSource conf /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual SkipNavigation SetInverse([CanBeNull] SkipNavigation inverse, ConfigurationSource configurationSource) + public virtual SkipNavigation? SetInverse([CanBeNull] SkipNavigation? inverse, ConfigurationSource configurationSource) { var oldInverse = Inverse; var isChanging = inverse != Inverse; @@ -310,8 +309,8 @@ public virtual void SetIsEagerLoaded(bool? eagerLoaded, ConfigurationSource conf /// The annotation that was set. protected override IConventionAnnotation OnAnnotationSet( string name, - IConventionAnnotation annotation, - IConventionAnnotation oldAnnotation) + IConventionAnnotation? annotation, + IConventionAnnotation? oldAnnotation) => DeclaringType.Model.ConventionDispatcher.OnSkipNavigationAnnotationChanged( Builder, name, annotation, oldAnnotation); @@ -357,14 +356,14 @@ public override string ToString() => this.ToDebugString(MetadataDebugStringOptions.SingleLineDefault); /// - IConventionSkipNavigationBuilder IConventionSkipNavigation.Builder + IConventionSkipNavigationBuilder? IConventionSkipNavigation.Builder { [DebuggerStepThrough] get => Builder; } /// - IConventionAnnotatableBuilder IConventionAnnotatable.Builder + IConventionAnnotatableBuilder? IConventionAnnotatable.Builder { [DebuggerStepThrough] get => Builder; @@ -387,39 +386,41 @@ IEntityType INavigationBase.TargetEntityType /// IForeignKey ISkipNavigation.ForeignKey { + // ModelValidator makes sure ForeignKey isn't null, so we expose it as non-nullable. [DebuggerStepThrough] - get => ForeignKey; + get => ForeignKey!; } /// [DebuggerStepThrough] - void IMutableSkipNavigation.SetForeignKey([CanBeNull] IMutableForeignKey foreignKey) - => SetForeignKey((ForeignKey)foreignKey, ConfigurationSource.Explicit); + void IMutableSkipNavigation.SetForeignKey(IMutableForeignKey? foreignKey) + => SetForeignKey((ForeignKey?)foreignKey, ConfigurationSource.Explicit); /// [DebuggerStepThrough] - IConventionForeignKey IConventionSkipNavigation.SetForeignKey([CanBeNull] IConventionForeignKey foreignKey, bool fromDataAnnotation) + IConventionForeignKey? IConventionSkipNavigation.SetForeignKey(IConventionForeignKey? foreignKey, bool fromDataAnnotation) => SetForeignKey( - (ForeignKey)foreignKey, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); + (ForeignKey?)foreignKey, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); /// ISkipNavigation ISkipNavigation.Inverse { + // ModelValidator makes sure ForeignKey isn't null, so we expose it as non-nullable. [DebuggerStepThrough] - get => Inverse; + get => Inverse!; } /// [DebuggerStepThrough] - IMutableSkipNavigation IMutableSkipNavigation.SetInverse([CanBeNull] IMutableSkipNavigation inverse) - => SetInverse((SkipNavigation)inverse, ConfigurationSource.Explicit); + IMutableSkipNavigation? IMutableSkipNavigation.SetInverse(IMutableSkipNavigation? inverse) + => SetInverse((SkipNavigation?)inverse, ConfigurationSource.Explicit); /// [DebuggerStepThrough] - IConventionSkipNavigation IConventionSkipNavigation.SetInverse( - [CanBeNull] IConventionSkipNavigation inverse, + IConventionSkipNavigation? IConventionSkipNavigation.SetInverse( + IConventionSkipNavigation? inverse, bool fromDataAnnotation) => SetInverse( - (SkipNavigation)inverse, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); + (SkipNavigation?)inverse, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention); } } diff --git a/src/EFCore/Metadata/Internal/SkipNavigationComparer.cs b/src/EFCore/Metadata/Internal/SkipNavigationComparer.cs index a5a23edb108..e822e23a782 100644 --- a/src/EFCore/Metadata/Internal/SkipNavigationComparer.cs +++ b/src/EFCore/Metadata/Internal/SkipNavigationComparer.cs @@ -4,6 +4,8 @@ using System; using System.Collections.Generic; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// diff --git a/src/EFCore/Metadata/Internal/SkipNavigationExtensions.cs b/src/EFCore/Metadata/Internal/SkipNavigationExtensions.cs index d4c4fd0fe7a..6095477522c 100644 --- a/src/EFCore/Metadata/Internal/SkipNavigationExtensions.cs +++ b/src/EFCore/Metadata/Internal/SkipNavigationExtensions.cs @@ -3,6 +3,8 @@ using JetBrains.Annotations; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Metadata.Internal { /// @@ -19,7 +21,7 @@ public static class SkipNavigationExtensions /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static MemberIdentity CreateMemberIdentity([CanBeNull] this ISkipNavigation navigation) + public static MemberIdentity CreateMemberIdentity([CanBeNull] this ISkipNavigation? navigation) => navigation?.GetIdentifyingMemberInfo() == null ? MemberIdentity.Create(navigation?.Name) : MemberIdentity.Create(navigation.GetIdentifyingMemberInfo()); diff --git a/src/EFCore/Properties/CoreStrings.Designer.cs b/src/EFCore/Properties/CoreStrings.Designer.cs index 859b9eb0d17..67be8268d71 100644 --- a/src/EFCore/Properties/CoreStrings.Designer.cs +++ b/src/EFCore/Properties/CoreStrings.Designer.cs @@ -1619,6 +1619,14 @@ public static string NavigationToKeylessType([CanBeNull] object navigation, [Can GetString("NavigationToKeylessType", nameof(navigation), nameof(entityType)), navigation, entityType); + /// + /// The navigation '{navigation}' cannot be added to the entity type '{entityType}' because it is defined in shadow state, and navigations properties cannot originate from shadow state entities. + /// + public static string NavigationFromShadowEntity([CanBeNull] object navigation, [CanBeNull] object entityType) + => string.Format( + GetString("NavigationFromShadowEntity", nameof(navigation), nameof(entityType)), + navigation, entityType); + /// /// The navigation '{navigation}' cannot be added to the entity type '{entityType}' because the target entity type '{targetType}' is defined in shadow state, and navigations properties cannot point to shadow state entities. /// diff --git a/src/EFCore/Properties/CoreStrings.resx b/src/EFCore/Properties/CoreStrings.resx index 17368a3e71b..c788b4726a7 100644 --- a/src/EFCore/Properties/CoreStrings.resx +++ b/src/EFCore/Properties/CoreStrings.resx @@ -1024,6 +1024,9 @@ The navigation '{navigation}' cannot be added because it targets the keyless entity type '{entityType}'. Navigations can only target entity types with keys. + + The navigation '{navigation}' cannot be added to the entity type '{entityType}' because it is defined in shadow state, and navigations properties cannot originate from shadow state entities. + The navigation '{navigation}' cannot be added to the entity type '{entityType}' because the target entity type '{targetType}' is defined in shadow state, and navigations properties cannot point to shadow state entities. diff --git a/src/EFCore/Query/EntityShaperExpression.cs b/src/EFCore/Query/EntityShaperExpression.cs index 5e3ae2344c5..09ac90e8c40 100644 --- a/src/EFCore/Query/EntityShaperExpression.cs +++ b/src/EFCore/Query/EntityShaperExpression.cs @@ -129,7 +129,7 @@ protected virtual LambdaExpression GenerateMaterializationCondition([NotNull] IE var exception = CreateUnableToDiscriminateExceptionExpression(entityType, discriminatorValueVariable); - var discriminatorComparer = discriminatorProperty.GetKeyValueComparer(); + var discriminatorComparer = discriminatorProperty.GetKeyValueComparer()!; if (discriminatorComparer.IsDefault()) { var switchCases = new SwitchCase[concreteEntityTypes.Length]; diff --git a/src/EFCore/Query/Internal/EntityMaterializerSource.cs b/src/EFCore/Query/Internal/EntityMaterializerSource.cs index 0dd6fa1defb..872a61e1e47 100644 --- a/src/EFCore/Query/Internal/EntityMaterializerSource.cs +++ b/src/EFCore/Query/Internal/EntityMaterializerSource.cs @@ -125,7 +125,7 @@ var blockExpressions var readValueExpression = property is IServiceProperty serviceProperty - ? serviceProperty.GetParameterBinding().BindToParameter(bindingInfo) + ? serviceProperty.GetParameterBinding()!.BindToParameter(bindingInfo) : valueBufferExpression.CreateValueBufferReadValueExpression( memberInfo.GetMemberType(), property.GetIndex(), @@ -151,9 +151,9 @@ static Expression CreateMemberAssignment(Expression parameter, MemberInfo member private ConcurrentDictionary> Materializers => LazyInitializer.EnsureInitialized( - // TODO: Even though we should be able to pass nullable here for some reason it is inferring generic type incorrectly - ref _materializers!, - () => new ConcurrentDictionary>()); + // TODO-NULLABLE: LazyInitializer not yet null-annotated in netstandard2.1, can remove bang after targeting net5.0 + ref _materializers, + () => new ConcurrentDictionary>())!; /// /// 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 190661641d3..3a62081ffe9 100644 --- a/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs +++ b/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs @@ -1646,9 +1646,12 @@ private static IEnumerable FindNavigations(IEntityType entityTy else { foreach (var derivedNavigation in entityType.GetDerivedTypes() - .Select(et => et.FindDeclaredNavigation(navigationName)).Where(n => n != null)) + .Select(et => et.FindDeclaredNavigation(navigationName))) { - yield return derivedNavigation; + if (derivedNavigation != null) + { + yield return derivedNavigation; + } } } diff --git a/src/Shared/Check.cs b/src/Shared/Check.cs index 0a3ab5dc2da..3403a81c904 100644 --- a/src/Shared/Check.cs +++ b/src/Shared/Check.cs @@ -9,13 +9,15 @@ using Microsoft.EntityFrameworkCore.Diagnostics; using CA = System.Diagnostics.CodeAnalysis; +#nullable enable + namespace Microsoft.EntityFrameworkCore.Utilities { [DebuggerStepThrough] internal static class Check { [ContractAnnotation("value:null => halt")] - public static T NotNull([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName) + public static T NotNull([NoEnumeration, CA.AllowNull, CA.NotNull] T value, [InvokerParameterName] [NotNull] string parameterName) { #pragma warning disable IDE0041 // Use 'is null' check if (ReferenceEquals(value, null)) @@ -30,7 +32,8 @@ public static T NotNull([NoEnumeration] T value, [InvokerParameterName] [NotN } [ContractAnnotation("value:null => halt")] - public static IReadOnlyList NotEmpty(IReadOnlyList value, [InvokerParameterName] [NotNull] string parameterName) + public static IReadOnlyList NotEmpty( + [CA.NotNull] IReadOnlyList? value, [InvokerParameterName] [NotNull] string parameterName) { NotNull(value, parameterName); @@ -45,29 +48,24 @@ public static IReadOnlyList NotEmpty(IReadOnlyList value, [InvokerParam } [ContractAnnotation("value:null => halt")] - public static string NotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName) + public static string NotEmpty([CA.NotNull] string? value, [InvokerParameterName] [NotNull] string parameterName) { - Exception e = null; if (value is null) { - e = new ArgumentNullException(parameterName); - } - else if (value.Trim().Length == 0) - { - e = new ArgumentException(AbstractionsStrings.ArgumentIsEmpty(parameterName)); + NotEmpty(parameterName, nameof(parameterName)); + throw new ArgumentNullException(parameterName); } - if (e != null) + if (value.Trim().Length == 0) { NotEmpty(parameterName, nameof(parameterName)); - - throw e; + throw new ArgumentException(AbstractionsStrings.ArgumentIsEmpty(parameterName)); } return value; } - public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName) + public static string? NullButNotEmpty(string? value, [InvokerParameterName] [NotNull] string parameterName) { if (!(value is null) && value.Length == 0) @@ -80,7 +78,8 @@ public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNu return value; } - public static IReadOnlyList HasNoNulls(IReadOnlyList value, [InvokerParameterName] [NotNull] string parameterName) + public static IReadOnlyList HasNoNulls( + [CA.NotNull] IReadOnlyList? value, [InvokerParameterName] [NotNull] string parameterName) where T : class { NotNull(value, parameterName); @@ -96,7 +95,7 @@ public static IReadOnlyList HasNoNulls(IReadOnlyList value, [InvokerPar } public static IReadOnlyList HasNoEmptyElements( - IReadOnlyList value, + [CA.NotNull] IReadOnlyList? value, [InvokerParameterName] [NotNull] string parameterName) { NotNull(value, parameterName); diff --git a/test/EFCore.Tests/Extensions/PropertyExtensionsTest.cs b/test/EFCore.Tests/Extensions/PropertyExtensionsTest.cs index b796953695a..a14fc16d266 100644 --- a/test/EFCore.Tests/Extensions/PropertyExtensionsTest.cs +++ b/test/EFCore.Tests/Extensions/PropertyExtensionsTest.cs @@ -93,7 +93,7 @@ public void Get_generation_property_returns_null_for_property_without_generator( var entityType = model.AddEntityType("Entity"); var property = entityType.AddProperty("Property", typeof(int)); - Assert.Null(property.GetGenerationProperty()); + Assert.Null(property.FindGenerationProperty()); } [ConditionalFact] @@ -107,7 +107,7 @@ public void Get_generation_property_returns_same_property_on_property_with_gener property.ValueGenerated = ValueGenerated.OnAdd; - Assert.Equal(property, property.GetGenerationProperty()); + Assert.Equal(property, property.FindGenerationProperty()); } [ConditionalFact] @@ -130,7 +130,7 @@ public void Get_generation_property_returns_generation_property_from_foreign_key firstProperty.ValueGenerated = ValueGenerated.OnAdd; - Assert.Equal(firstProperty, thirdProperty.GetGenerationProperty()); + Assert.Equal(firstProperty, thirdProperty.FindGenerationProperty()); } [ConditionalFact] @@ -161,7 +161,7 @@ public void Get_generation_property_returns_generation_property_from_foreign_key rightId2.ValueGenerated = ValueGenerated.OnAdd; - Assert.Equal(rightId2, endProperty.GetGenerationProperty()); + Assert.Equal(rightId2, endProperty.FindGenerationProperty()); } [ConditionalFact] @@ -189,7 +189,7 @@ public void Get_generation_property_returns_generation_property_from_foreign_key leafId1.ValueGenerated = ValueGenerated.OnAdd; - Assert.Equal(leafId1, secondId1.GetGenerationProperty()); + Assert.Equal(leafId1, secondId1.FindGenerationProperty()); } [ConditionalFact] @@ -199,19 +199,19 @@ public void Get_generation_property_for_one_to_one_FKs() Assert.Equal( model.FindEntityType(typeof(Product)).FindProperty("Id"), - model.FindEntityType(typeof(ProductDetails)).GetForeignKeys().Single().Properties[0].GetGenerationProperty()); + model.FindEntityType(typeof(ProductDetails)).GetForeignKeys().Single().Properties[0].FindGenerationProperty()); Assert.Equal( model.FindEntityType(typeof(Product)).FindProperty("Id"), - model.FindEntityType(typeof(ProductDetailsTag)).GetForeignKeys().Single().Properties[0].GetGenerationProperty()); + model.FindEntityType(typeof(ProductDetailsTag)).GetForeignKeys().Single().Properties[0].FindGenerationProperty()); Assert.Equal( model.FindEntityType(typeof(ProductDetails)).FindProperty("Id2"), - model.FindEntityType(typeof(ProductDetailsTag)).GetForeignKeys().Single().Properties[1].GetGenerationProperty()); + model.FindEntityType(typeof(ProductDetailsTag)).GetForeignKeys().Single().Properties[1].FindGenerationProperty()); Assert.Equal( model.FindEntityType(typeof(ProductDetails)).FindProperty("Id2"), - model.FindEntityType(typeof(ProductDetailsTagDetails)).GetForeignKeys().Single().Properties[0].GetGenerationProperty()); + model.FindEntityType(typeof(ProductDetailsTagDetails)).GetForeignKeys().Single().Properties[0].FindGenerationProperty()); } [ConditionalFact] @@ -222,12 +222,12 @@ public void Get_generation_property_for_one_to_many_identifying_FKs() Assert.Equal( model.FindEntityType(typeof(Order)).FindProperty("Id"), model.FindEntityType(typeof(OrderDetails)).GetForeignKeys().Single(k => k.Properties.First().Name == "OrderId") - .Properties[0].GetGenerationProperty()); + .Properties[0].FindGenerationProperty()); Assert.Equal( model.FindEntityType(typeof(Product)).FindProperty("Id"), model.FindEntityType(typeof(OrderDetails)).GetForeignKeys().Single(k => k.Properties.First().Name == "ProductId") - .Properties[0].GetGenerationProperty()); + .Properties[0].FindGenerationProperty()); } private class Category