diff --git a/src/EFCore/Query/QueryCompilationContext.cs b/src/EFCore/Query/QueryCompilationContext.cs index 71d60801dc0..4746ff9995f 100644 --- a/src/EFCore/Query/QueryCompilationContext.cs +++ b/src/EFCore/Query/QueryCompilationContext.cs @@ -32,14 +32,12 @@ public QueryCompilationContext( QueryCompilationContextDependencies dependencies, bool async) { - var context = dependencies.CurrentContext.Context; - IsAsync = async; - IsTracking = context.ChangeTracker.QueryTrackingBehavior == QueryTrackingBehavior.TrackAll; + IsTracking = dependencies.IsTracking; IsBuffering = dependencies.IsRetryingExecutionStrategy; Model = dependencies.Model; ContextOptions = dependencies.ContextOptions; - ContextType = context.GetType(); + ContextType = dependencies.ContextType; Logger = dependencies.Logger; _queryTranslationPreprocessorFactory = dependencies.QueryTranslationPreprocessorFactory; diff --git a/src/EFCore/Query/QueryCompilationContextDependencies.cs b/src/EFCore/Query/QueryCompilationContextDependencies.cs index 95101aa3623..85e701248e2 100644 --- a/src/EFCore/Query/QueryCompilationContextDependencies.cs +++ b/src/EFCore/Query/QueryCompilationContextDependencies.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -37,6 +38,7 @@ namespace Microsoft.EntityFrameworkCore.Query public sealed class QueryCompilationContextDependencies { private readonly IExecutionStrategyFactory _executionStrategyFactory; + private readonly ICurrentDbContext _currentContext; /// /// @@ -79,7 +81,7 @@ public QueryCompilationContextDependencies( Check.NotNull(contextOptions, nameof(contextOptions)); Check.NotNull(logger, nameof(logger)); - CurrentContext = currentContext; + _currentContext = currentContext; Model = model; QueryTranslationPreprocessorFactory = queryTranslationPreprocessorFactory; QueryableMethodTranslatingExpressionVisitorFactory = queryableMethodTranslatingExpressionVisitorFactory; @@ -92,9 +94,14 @@ public QueryCompilationContextDependencies( } /// - /// The cache being used to store value generator instances. + /// The CLR type of DbContext. /// - public ICurrentDbContext CurrentContext { get; } + public Type ContextType => _currentContext.Context.GetType(); + + /// + /// The flag indicating if default query tracking behavior is tracking. + /// + public bool IsTracking => _currentContext.Context.ChangeTracker.QueryTrackingBehavior == QueryTrackingBehavior.TrackAll; /// /// The model. @@ -149,7 +156,7 @@ public QueryCompilationContextDependencies With([NotNull] IModel model) QueryTranslationPostprocessorFactory, ShapedQueryCompilingExpressionVisitorFactory, _executionStrategyFactory, - CurrentContext, + _currentContext, ContextOptions, Logger); @@ -166,7 +173,7 @@ public QueryCompilationContextDependencies With([NotNull] IQueryTranslationPrepr QueryTranslationPostprocessorFactory, ShapedQueryCompilingExpressionVisitorFactory, _executionStrategyFactory, - CurrentContext, + _currentContext, ContextOptions, Logger); @@ -184,7 +191,7 @@ public QueryCompilationContextDependencies With( QueryTranslationPostprocessorFactory, ShapedQueryCompilingExpressionVisitorFactory, _executionStrategyFactory, - CurrentContext, + _currentContext, ContextOptions, Logger); @@ -202,7 +209,7 @@ public QueryCompilationContextDependencies With( queryTranslationPostprocessorFactory, ShapedQueryCompilingExpressionVisitorFactory, _executionStrategyFactory, - CurrentContext, + _currentContext, ContextOptions, Logger); @@ -220,7 +227,7 @@ public QueryCompilationContextDependencies With( QueryTranslationPostprocessorFactory, shapedQueryCompilingExpressionVisitorFactory, _executionStrategyFactory, - CurrentContext, + _currentContext, ContextOptions, Logger); @@ -237,7 +244,7 @@ public QueryCompilationContextDependencies With([NotNull] IExecutionStrategyFact QueryTranslationPostprocessorFactory, ShapedQueryCompilingExpressionVisitorFactory, executionStrategyFactory, - CurrentContext, + _currentContext, ContextOptions, Logger); @@ -271,7 +278,7 @@ public QueryCompilationContextDependencies With([NotNull] IDbContextOptions cont QueryTranslationPostprocessorFactory, ShapedQueryCompilingExpressionVisitorFactory, _executionStrategyFactory, - CurrentContext, + _currentContext, contextOptions, Logger); @@ -288,7 +295,7 @@ public QueryCompilationContextDependencies With([NotNull] IDiagnosticsLogger(params string[] ignorePropertie .First(); var constructorParameters = constructor.GetParameters().Where(p => !obsoleteTypes.Contains(p.ParameterType)).ToList(); - Assert.Equal(constructorParameters.Count, serviceProperties.Count); - - foreach (var serviceType in constructorParameters.Where(p => !ignoreProperties.Contains(p.Name)).Select(p => p.ParameterType)) + foreach (var serviceType in constructorParameters.Select(p => p.ParameterType)) { var withMethod = typeof(TDependencies).GetTypeInfo().DeclaredMethods .Single( diff --git a/test/EFCore.Tests/Query/QueryCompilationContextDependenciesTest.cs b/test/EFCore.Tests/Query/QueryCompilationContextDependenciesTest.cs index 83cfe5c25ca..a2414682e7f 100644 --- a/test/EFCore.Tests/Query/QueryCompilationContextDependenciesTest.cs +++ b/test/EFCore.Tests/Query/QueryCompilationContextDependenciesTest.cs @@ -12,7 +12,9 @@ public class QueryCompilationContextDependenciesTest [ConditionalFact] public void Can_use_With_methods_to_clone_and_replace_service() { - InMemoryTestHelpers.Instance.TestDependenciesClone(); + InMemoryTestHelpers.Instance.TestDependenciesClone( + nameof(QueryCompilationContextDependencies.IsTracking), + nameof(QueryCompilationContextDependencies.ContextType)); } } }