3737using Microsoft . EntityFrameworkCore . Query . Expressions ;
3838using Remotion . Linq . Clauses ;
3939using Microsoft . EntityFrameworkCore . Query . Sql ;
40+ using Microsoft . EntityFrameworkCore . Diagnostics ;
4041
42+ #endif
43+ #if EFCORE_3X
44+ using IEvaluatableExpressionFilter = Microsoft . EntityFrameworkCore . Query . Internal . IEvaluatableExpressionFilter ;
4145#endif
4246
4347namespace Z . EntityFramework . Plus
@@ -89,6 +93,8 @@ public virtual void ExecuteInMemory()
8993 /// <returns>The new executor and get command.</returns>
9094 public virtual IRelationalCommand CreateExecutorAndGetCommand ( out RelationalQueryContext queryContext )
9195 {
96+ queryContext = null ;
97+
9298 bool isEFCore2x = false ;
9399 bool EFCore_2_1 = false ;
94100#if EFCORE
@@ -234,15 +240,11 @@ public virtual IRelationalCommand CreateExecutorAndGetCommand(out RelationalQuer
234240 var innerConnectionField = typeof ( RelationalConnection ) . GetField ( "_connection" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
235241 var initalConnection = innerConnectionField . GetValue ( QueryConnection ) ;
236242
237- if ( EFCoreHelper . IsVersion3xPreview5 )
238- {
239- innerConnectionField . SetValue ( QueryConnection , innerConnection ) ;
240- }
241- else
242- {
243- innerConnectionField . SetValue ( QueryConnection , LazyHelper . NewLazy < DbConnection > ( ( ) => innerConnection ) ) ;
244- }
245-
243+ #if EFCORE_3X
244+ innerConnectionField . SetValue ( QueryConnection , innerConnection ) ;
245+ #else
246+ innerConnectionField . SetValue ( QueryConnection , LazyHelper . NewLazy < DbConnection > ( ( ) => innerConnection ) ) ;
247+ #endif
246248
247249 RestoreConnection = ( ) => innerConnectionField . SetValue ( QueryConnection , initalConnection ) ;
248250 }
@@ -254,28 +256,45 @@ public virtual IRelationalCommand CreateExecutorAndGetCommand(out RelationalQuer
254256 var relationalQueryContextConstructor = relationalQueryContextType . GetConstructors ( ) [ 0 ] ;
255257
256258 // EF Core 1.1 preview
257- if ( relationalQueryContextConstructor . GetParameters ( ) . Length == 5 )
259+ if ( isEFCore3x )
258260 {
259261 // REFLECTION: Query.Provider._queryCompiler._queryContextFactory.ExecutionStrategyFactory
260262 var executionStrategyFactoryField = queryContextFactory . GetType ( ) . GetProperty ( "ExecutionStrategyFactory" , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . FlattenHierarchy ) ;
261263 var executionStrategyFactory = executionStrategyFactoryField . GetValue ( queryContextFactory ) ;
262264
263- var lazyRefStateManager = LazyHelper . NewLazy ( ( ) => stateManager ) ;
265+ var dependenciesProperty3 = typeof ( RelationalQueryContextFactory ) . GetProperty ( "Dependencies" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
266+ var dependencies3 = dependenciesProperty3 . GetValue ( queryContextFactory ) ;
267+
268+ queryContext = ( RelationalQueryContext ) relationalQueryContextConstructor . Invoke ( new object [ ] { dependencies3 , createQueryBufferDelegate , QueryConnection , executionStrategyFactory } ) ;
269+ }
270+ else if ( relationalQueryContextConstructor . GetParameters ( ) . Length == 5 )
271+ {
272+ // REFLECTION: Query.Provider._queryCompiler._queryContextFactory.ExecutionStrategyFactory
273+ var executionStrategyFactoryField = queryContextFactory . GetType ( ) . GetProperty ( "ExecutionStrategyFactory" , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . FlattenHierarchy ) ;
274+ var executionStrategyFactory = executionStrategyFactoryField . GetValue ( queryContextFactory ) ;
264275
276+ #if ! EFCORE_3X
277+ var lazyRefStateManager = LazyHelper . NewLazy ( ( ) => stateManager ) ;
265278 queryContext = ( RelationalQueryContext ) relationalQueryContextConstructor . Invoke ( new object [ ] { createQueryBufferDelegate , QueryConnection , lazyRefStateManager , concurrencyDetector , executionStrategyFactory } ) ;
279+ #endif
280+
281+
282+
266283 }
267284 else if ( isEFCore2x )
268285 {
269286 // REFLECTION: Query.Provider._queryCompiler._queryContextFactory.ExecutionStrategyFactory
270287 var executionStrategyFactoryField = queryContextFactory . GetType ( ) . GetProperty ( "ExecutionStrategyFactory" , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . FlattenHierarchy ) ;
271288 var executionStrategyFactory = executionStrategyFactoryField . GetValue ( queryContextFactory ) ;
272289
290+ #if ! EFCORE_3X
273291 var lazyRefStateManager = LazyHelper . NewLazy ( ( ) => stateManager ) ;
274292
275293 var dependenciesProperty3 = typeof ( RelationalQueryContextFactory ) . GetProperty ( "Dependencies" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
276294 var dependencies3 = dependenciesProperty3 . GetValue ( queryContextFactory ) ;
277295
278296 queryContext = ( RelationalQueryContext ) relationalQueryContextConstructor . Invoke ( new object [ ] { dependencies3 , createQueryBufferDelegate , QueryConnection , executionStrategyFactory } ) ;
297+ #endif
279298 }
280299 else
281300 {
@@ -284,16 +303,18 @@ public virtual IRelationalCommand CreateExecutorAndGetCommand(out RelationalQuer
284303 }
285304
286305
287- Expression newQuery ;
306+ Expression newQuery = null ;
288307
289308 if ( isEFCore3x )
290309 {
291- var parameterExtractingExpressionVisitorConstructor = typeof ( ParameterExtractingExpressionVisitor ) . GetConstructors ( ) . First ( x => x . GetParameters ( ) . Length == 6 ) ;
292-
293- var parameterExtractingExpressionVisitor = ( ParameterExtractingExpressionVisitor ) parameterExtractingExpressionVisitorConstructor . Invoke ( new object [ ] { evaluatableExpressionFilter , queryContext , queryContext . GetType ( ) , logger , true , false } ) ;
294-
295- // CREATE new query from query visitor
296- newQuery = parameterExtractingExpressionVisitor . ExtractParameters ( Query . Expression ) ;
310+ #if EFCORE_3X
311+ var visitor = new ParameterExtractingExpressionVisitor ( evaluatableExpressionFilter , queryContext , queryContext . GetType ( ) , ( IDiagnosticsLogger < DbLoggerCategory . Query > ) logger , true , false ) ;
312+ newQuery = visitor . ExtractParameters ( Query . Expression ) ;
313+ #endif
314+ //var parameterExtractingExpressionVisitorConstructor = typeof(ParameterExtractingExpressionVisitor).GetConstructors().First(x => x.GetParameters().Length == 6);
315+ //var parameterExtractingExpressionVisitor = (ParameterExtractingExpressionVisitor)parameterExtractingExpressionVisitorConstructor.Invoke(new object[] { evaluatableExpressionFilter, queryContext, queryContext.GetType(), logger, true, false });
316+ //// CREATE new query from query visitor
317+ //newQuery = parameterExtractingExpressionVisitor.ExtractParameters(Query.Expression);
297318 }
298319 else if ( isEFCore2x )
299320 {
@@ -394,8 +415,8 @@ public virtual IRelationalCommand CreateExecutorAndGetCommand(out RelationalQuer
394415 }
395416#endif
396417
397- /// <summary>Sets the result of the query deferred.</summary>
398- /// <param name="reader">The reader returned from the query execution.</param>
418+ /// <summary>Sets the result of the query deferred.</summary>
419+ /// <param name="reader">The reader returned from the query execution.</param>
399420 public virtual void SetResult ( DbDataReader reader )
400421 {
401422 }
0 commit comments