From efd79b2bad48beffa9c249b410baef8cae86b842 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Fri, 6 Oct 2023 10:15:09 +0100 Subject: [PATCH] Remove quirks from main (#31976) --- .../Query/SqlExpressions/SelectExpression.cs | 5 +- src/EFCore/Metadata/RuntimeEntityType.cs | 17 +-- .../Internal/EntityMaterializerSource.cs | 119 +++++++----------- .../ValueGeneration/ValueGeneratorCache.cs | 34 +---- 4 files changed, 60 insertions(+), 115 deletions(-) diff --git a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs index 1faa9063b77..ff41e2bc134 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs @@ -25,9 +25,6 @@ namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions; public sealed partial class SelectExpression : TableExpressionBase { private const string DiscriminatorColumnAlias = "Discriminator"; - private static readonly bool UseOldBehavior31107 = - AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31107", out var enabled31107) && enabled31107; - private static readonly IdentifierComparer IdentifierComparerInstance = new(); private readonly List _projection = new(); @@ -2976,7 +2973,7 @@ static IReadOnlyDictionary GetPropertyExpressions( || (derivedType && ownerType.GetMappingStrategy() != RelationalAnnotationNames.TphMappingStrategy); var newColumnsNullable = pkColumnsNullable || !navigation.ForeignKey.IsRequiredDependent - || (derivedType && !UseOldBehavior31107); + || derivedType; if (derivedTpt) { principalMappings = principalMappings.Except(ownerType.BaseType!.GetViewOrTableMappings().Select(e => e.Table)); diff --git a/src/EFCore/Metadata/RuntimeEntityType.cs b/src/EFCore/Metadata/RuntimeEntityType.cs index 09e847e831c..79bbd12c4ee 100644 --- a/src/EFCore/Metadata/RuntimeEntityType.cs +++ b/src/EFCore/Metadata/RuntimeEntityType.cs @@ -6,7 +6,6 @@ using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Metadata.Internal; -using Microsoft.EntityFrameworkCore.Query.Internal; namespace Microsoft.EntityFrameworkCore.Metadata; @@ -845,11 +844,9 @@ static IEnumerable Create(RuntimeEntityType type) /// [EntityFrameworkInternal] public virtual Func GetOrCreateMaterializer(IEntityMaterializerSource source) - => EntityMaterializerSource.UseOldBehavior31866 - ? source.GetMaterializer(this) - : NonCapturingLazyInitializer.EnsureInitialized( - ref _materializer, this, source, - static (e, s) => s.GetMaterializer(e)); + => NonCapturingLazyInitializer.EnsureInitialized( + ref _materializer, this, source, + static (e, s) => s.GetMaterializer(e)); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -859,11 +856,9 @@ public virtual Func GetOrCreateMaterializer(IEnt /// [EntityFrameworkInternal] public virtual Func GetOrCreateEmptyMaterializer(IEntityMaterializerSource source) - => EntityMaterializerSource.UseOldBehavior31866 - ? source.GetEmptyMaterializer(this) - : NonCapturingLazyInitializer.EnsureInitialized( - ref _emptyMaterializer, this, source, - static (e, s) => s.GetEmptyMaterializer(e)); + => NonCapturingLazyInitializer.EnsureInitialized( + ref _emptyMaterializer, this, source, + static (e, s) => s.GetEmptyMaterializer(e)); /// /// Returns a string that represents the current object. diff --git a/src/EFCore/Query/Internal/EntityMaterializerSource.cs b/src/EFCore/Query/Internal/EntityMaterializerSource.cs index b1a72bc24c3..4a67bd00569 100644 --- a/src/EFCore/Query/Internal/EntityMaterializerSource.cs +++ b/src/EFCore/Query/Internal/EntityMaterializerSource.cs @@ -15,15 +15,6 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal; /// public class EntityMaterializerSource : IEntityMaterializerSource { - /// - /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to - /// the same compatibility standards as public APIs. It may be changed or removed without notice in - /// 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 readonly bool UseOldBehavior31866 = - AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31866", out var enabled31866) && enabled31866; - private static readonly MethodInfo InjectableServiceInjectedMethod = typeof(IInjectableService).GetMethod(nameof(IInjectableService.Injected))!; @@ -486,21 +477,14 @@ private ConcurrentDictionary> public virtual Func GetMaterializer( IEntityType entityType) { - return UseOldBehavior31866 - ? Materializers.GetOrAdd(entityType, static (e, s) => CreateMaterializer(s, e), this) - : CreateMaterializer(this, entityType); - - static Func CreateMaterializer(EntityMaterializerSource self, IEntityType e) - { - var materializationContextParameter - = Expression.Parameter(typeof(MaterializationContext), "materializationContext"); - - return Expression.Lambda>( - ((IEntityMaterializerSource)self).CreateMaterializeExpression( - new EntityMaterializerSourceParameters(e, "instance", null), materializationContextParameter), - materializationContextParameter) - .Compile(); - } + var materializationContextParameter + = Expression.Parameter(typeof(MaterializationContext), "materializationContext"); + + return Expression.Lambda>( + ((IEntityMaterializerSource)this).CreateMaterializeExpression( + new EntityMaterializerSourceParameters(entityType, "instance", null), materializationContextParameter), + materializationContextParameter) + .Compile(); } private ConcurrentDictionary> EmptyMaterializers @@ -516,61 +500,54 @@ private ConcurrentDictionary> /// public virtual Func GetEmptyMaterializer(IEntityType entityType) { - return UseOldBehavior31866 - ? EmptyMaterializers.GetOrAdd(entityType, static (e, s) => CreateEmptyMaterializer(s, e), this) - : CreateEmptyMaterializer(this, entityType); - - static Func CreateEmptyMaterializer(EntityMaterializerSource self, IEntityType e) + var binding = entityType.ServiceOnlyConstructorBinding; + if (binding == null) { - var binding = e.ServiceOnlyConstructorBinding; - if (binding == null) - { - var _ = e.ConstructorBinding; - binding = e.ServiceOnlyConstructorBinding; - if (binding == null) - { - throw new InvalidOperationException(CoreStrings.NoParameterlessConstructor(e.DisplayName())); - } - } + var _ = entityType.ConstructorBinding; + binding = entityType.ServiceOnlyConstructorBinding; + if (binding == null) + { + throw new InvalidOperationException(CoreStrings.NoParameterlessConstructor(entityType.DisplayName())); + } + } - binding = self.ModifyBindings(e, binding); + binding = ModifyBindings(entityType, binding); - var materializationContextExpression = Expression.Parameter(typeof(MaterializationContext), "mc"); - var bindingInfo = new ParameterBindingInfo( - new EntityMaterializerSourceParameters(e, "instance", null), materializationContextExpression); + var materializationContextExpression = Expression.Parameter(typeof(MaterializationContext), "mc"); + var bindingInfo = new ParameterBindingInfo( + new EntityMaterializerSourceParameters(entityType, "instance", null), materializationContextExpression); - var blockExpressions = new List(); - var instanceVariable = Expression.Variable(binding.RuntimeType, "instance"); - var serviceProperties = e.GetServiceProperties().ToList(); - bindingInfo.ServiceInstances.Add(instanceVariable); + var blockExpressions = new List(); + var instanceVariable = Expression.Variable(binding.RuntimeType, "instance"); + var serviceProperties = entityType.GetServiceProperties().ToList(); + bindingInfo.ServiceInstances.Add(instanceVariable); - CreateServiceInstances(binding, bindingInfo, blockExpressions, serviceProperties); + CreateServiceInstances(binding, bindingInfo, blockExpressions, serviceProperties); - var constructorExpression = binding.CreateConstructorExpression(bindingInfo); + var constructorExpression = binding.CreateConstructorExpression(bindingInfo); - var properties = new HashSet(serviceProperties); - foreach (var consumedProperty in binding.ParameterBindings.SelectMany(p => p.ConsumedProperties)) - { - properties.Remove(consumedProperty); - } - - return Expression.Lambda>( - self._materializationInterceptor == null - ? properties.Count == 0 && blockExpressions.Count == 0 - ? constructorExpression - : self.CreateMaterializeExpression( - blockExpressions, instanceVariable, constructorExpression, properties, bindingInfo) - : self.CreateInterceptionMaterializeExpression( - e, - new HashSet(), - self._materializationInterceptor, - bindingInfo, - constructorExpression, - instanceVariable, - blockExpressions), - materializationContextExpression) - .Compile(); + var properties = new HashSet(serviceProperties); + foreach (var consumedProperty in binding.ParameterBindings.SelectMany(p => p.ConsumedProperties)) + { + properties.Remove(consumedProperty); } + + return Expression.Lambda>( + _materializationInterceptor == null + ? properties.Count == 0 && blockExpressions.Count == 0 + ? constructorExpression + : CreateMaterializeExpression( + blockExpressions, instanceVariable, constructorExpression, properties, bindingInfo) + : CreateInterceptionMaterializeExpression( + entityType, + new HashSet(), + _materializationInterceptor, + bindingInfo, + constructorExpression, + instanceVariable, + blockExpressions), + materializationContextExpression) + .Compile(); } private InstantiationBinding ModifyBindings(ITypeBase structuralType, InstantiationBinding binding) diff --git a/src/EFCore/ValueGeneration/ValueGeneratorCache.cs b/src/EFCore/ValueGeneration/ValueGeneratorCache.cs index 6d4ee28fb2a..147d52df8cf 100644 --- a/src/EFCore/ValueGeneration/ValueGeneratorCache.cs +++ b/src/EFCore/ValueGeneration/ValueGeneratorCache.cs @@ -27,9 +27,6 @@ namespace Microsoft.EntityFrameworkCore.ValueGeneration; /// public class ValueGeneratorCache : IValueGeneratorCache { - private static readonly bool _useOldBehavior31539 = - AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31539", out var enabled31539) && enabled31539; - /// /// Initializes a new instance of the class. /// @@ -54,32 +51,13 @@ public ValueGeneratorCache(ValueGeneratorCacheDependencies dependencies) public CacheKey(IProperty property, ITypeBase typeBase) { - if (_useOldBehavior31539) - { - _modelId = default; - _property = null; - _typeBase = null; - Property = property; - TypeBase = typeBase; - } - else - { - _modelId = typeBase.Model.ModelId; - _property = property.Name; - _typeBase = typeBase.Name; - Property = null; - TypeBase = null; - } + _modelId = typeBase.Model.ModelId; + _property = property.Name; + _typeBase = typeBase.Name; } - public IProperty? Property { get; } - - public ITypeBase? TypeBase { get; } - public bool Equals(CacheKey other) - => _useOldBehavior31539 - ? Property!.Equals(other.Property) && TypeBase!.Equals(other.TypeBase) - : (_property!.Equals(other._property, StringComparison.Ordinal) + => (_property!.Equals(other._property, StringComparison.Ordinal) && _typeBase!.Equals(other._typeBase, StringComparison.Ordinal) && _modelId.Equals(other._modelId)); @@ -87,9 +65,7 @@ public override bool Equals(object? obj) => obj is CacheKey cacheKey && Equals(cacheKey); public override int GetHashCode() - => _useOldBehavior31539 - ? HashCode.Combine(Property!, TypeBase!) - : HashCode.Combine(_property!, _typeBase!, _modelId); + => HashCode.Combine(_property!, _typeBase!, _modelId); } ///