From e327d2cb82b868f9a8eb7c0d1fa7bcc77e69a1b4 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Wed, 1 Jul 2020 17:28:51 -0700 Subject: [PATCH] Query: Self-bootstrapping functions returning IQueryable (#21487) Public method FromExpression on DbContext to tie up our query provider with the expression Resolves #9810 --- src/EFCore/DbContext.cs | 11 ++++------- .../Query/UdfDbFunctionTestBase.cs | 8 ++++---- test/EFCore.Tests/DbContextTest.cs | 2 +- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/EFCore/DbContext.cs b/src/EFCore/DbContext.cs index c6393ea443b..95da9d7f6f8 100644 --- a/src/EFCore/DbContext.cs +++ b/src/EFCore/DbContext.cs @@ -1707,16 +1707,13 @@ public virtual ValueTask FindAsync([CanBeNull] object[] keyVal IServiceProvider IInfrastructure.Instance => InternalServiceProvider; /// - /// Creates a query expression, which represents a function call, against the query store. + /// Creates a queryable for given query expression. /// - /// The result type of the query expression + /// The result type of the query expression. /// The query expression to create. - /// An IQueryable representing the query. - protected virtual IQueryable CreateQuery([NotNull] Expression>> expression) + /// An representing the query. + public virtual IQueryable FromExpression([NotNull] Expression>> expression) { - //should we add this method as an extension in relational? That would require making DbContextDependencies public. - //Is there a 3rd way? - Check.NotNull(expression, nameof(expression)); return DbContextDependencies.QueryProvider.CreateQuery(expression.Body); diff --git a/test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs b/test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs index 80e204df312..496b9b680dc 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs @@ -171,7 +171,7 @@ public class MultProductOrders public IQueryable GetCustomerOrderCountByYear(int customerId) { - return CreateQuery(() => GetCustomerOrderCountByYear(customerId)); + return FromExpression(() => GetCustomerOrderCountByYear(customerId)); } public class TopSellingProduct @@ -184,17 +184,17 @@ public class TopSellingProduct public IQueryable GetTopTwoSellingProducts() { - return CreateQuery(() => GetTopTwoSellingProducts()); + return FromExpression(() => GetTopTwoSellingProducts()); } public IQueryable GetTopSellingProductsForCustomer(int customerId) { - return CreateQuery(() => GetTopSellingProductsForCustomer(customerId)); + return FromExpression(() => GetTopSellingProductsForCustomer(customerId)); } public IQueryable GetOrdersWithMultipleProducts(int customerId) { - return CreateQuery(() => GetOrdersWithMultipleProducts(customerId)); + return FromExpression(() => GetOrdersWithMultipleProducts(customerId)); } #endregion diff --git a/test/EFCore.Tests/DbContextTest.cs b/test/EFCore.Tests/DbContextTest.cs index 68f37fc4cbc..5ef70fc0f71 100644 --- a/test/EFCore.Tests/DbContextTest.cs +++ b/test/EFCore.Tests/DbContextTest.cs @@ -727,7 +727,7 @@ public async Task It_throws_object_disposed_exception(bool async) await Assert.ThrowsAsync(() => context.FindAsync(typeof(Random), 77).AsTask()); var methodCount = typeof(DbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Count(); - var expectedMethodCount = 42 + 1; + var expectedMethodCount = 42 + 2; Assert.True( methodCount == expectedMethodCount, userMessage: $"Expected {expectedMethodCount} methods on DbContext but found {methodCount}. "