From 0bd083b0755bba7c524a87bdac22633114a8f807 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Tue, 18 Sep 2018 15:14:54 -0700 Subject: [PATCH] Revert "Fix #11803 - QueryFilters: query filters are not applied recursively" This reverts commit 4a35f9ddedfdc6a8e52b52e8e709b3a4a5320c8a. Resolves #13346 --- .../Query/RelationalQueryModelVisitor.cs | 4 +- ...odelExpressionApplyingExpressionVisitor.cs | 4 +- .../Query/QueryBugsTest.cs | 70 ++++++++++++++++++- .../QueryFilterFuncletizationSqlServerTest.cs | 19 ++--- 4 files changed, 78 insertions(+), 19 deletions(-) diff --git a/src/EFCore.Relational/Query/RelationalQueryModelVisitor.cs b/src/EFCore.Relational/Query/RelationalQueryModelVisitor.cs index 92502623b19..221de8915c9 100644 --- a/src/EFCore.Relational/Query/RelationalQueryModelVisitor.cs +++ b/src/EFCore.Relational/Query/RelationalQueryModelVisitor.cs @@ -191,8 +191,8 @@ protected override Expression VisitMethodCall(MethodCallExpression node) /// true if the target type should have a defining query applied. public override bool ShouldApplyDefiningQuery(IEntityType entityType, IQuerySource querySource) { - return !(entityType.FindAnnotation(RelationalAnnotationNames.TableName) is ConventionalAnnotation tableNameAnnotation) - || tableNameAnnotation?.GetConfigurationSource() == ConfigurationSource.Convention + return (!(entityType.FindAnnotation(RelationalAnnotationNames.TableName) is ConventionalAnnotation tableNameAnnotation) + || tableNameAnnotation?.GetConfigurationSource() == ConfigurationSource.Convention) && QueryCompilationContext.QueryAnnotations .OfType() .All(a => a.QuerySource != querySource); diff --git a/src/EFCore/Query/ExpressionVisitors/Internal/ModelExpressionApplyingExpressionVisitor.cs b/src/EFCore/Query/ExpressionVisitors/Internal/ModelExpressionApplyingExpressionVisitor.cs index ec68d93cda8..0aa12a31f06 100644 --- a/src/EFCore/Query/ExpressionVisitors/Internal/ModelExpressionApplyingExpressionVisitor.cs +++ b/src/EFCore/Query/ExpressionVisitors/Internal/ModelExpressionApplyingExpressionVisitor.cs @@ -117,7 +117,7 @@ var parameterizedQuery parameterize: false, generateContextAccessors: true); - var subQueryModel = _queryModelGenerator.ParseQuery(Visit(parameterizedQuery)); + var subQueryModel = _queryModelGenerator.ParseQuery(parameterizedQuery); newExpression = new SubQueryExpression(subQueryModel); } @@ -143,7 +143,7 @@ var predicateExpression .Replace( oldParameterExpression, newParameterExpression, - Visit(parameterizedFilter.Body)); + parameterizedFilter.Body); var whereExpression = Expression.Call( diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs index 26cd729774b..874734dab7f 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs @@ -3954,12 +3954,12 @@ FROM [Factions] AS [f] WHERE EXISTS ( SELECT 1 FROM [Leaders] AS [l] - WHERE ([l].[Name] LIKE N'Bran' + N'%' AND (LEFT([l].[Name], LEN(N'Bran')) = N'Bran')) AND ([l].[Name] = N'Crach an Craite'))"); + WHERE [l].[Name] = N'Crach an Craite')"); } } } - [Fact] + [Fact(Skip = "Issue#13361")] public virtual void Query_type_used_inside_defining_query() { using (CreateDatabase11803()) @@ -5135,7 +5135,73 @@ public class Address13157 public class AddressTurnovers13157 { public int AmountIn { get; set; } + } + + #endregion + + #region Bug13346 + + [Fact] + public virtual void ToQuery_can_define_in_own_terms_using_FromSql() + { + using (CreateDatabase13346()) + { + using (var context = new MyContext13346(_options)) + { + var query = context.Query().ToList(); + + Assert.Equal(4, query.Count); + AssertSql( + @"SELECT o.Amount From Orders AS o"); + } + } + } + + private SqlServerTestStore CreateDatabase13346() + { + return CreateTestStore( + () => new MyContext13346(_options), + context => + { + context.AddRange( + new Order13346 { Amount = 1}, + new Order13346 { Amount = 2}, + new Order13346 { Amount = 3}, + new Order13346 { Amount = 4} + ); + + context.SaveChanges(); + ClearLog(); + }); + } + + public class MyContext13346 : DbContext + { + public virtual DbSet Orders { get; set; } + + public MyContext13346(DbContextOptions options) + : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Query().ToQuery( + () => Query() + .FromSql("SELECT o.Amount From Orders AS o")); + } + } + + public class Order13346 + { + public int Id { get; set; } + public int Amount { get; set; } + } + + public class OrderSummary13346 + { + public int Amount { get; set; } } #endregion diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/QueryFilterFuncletizationSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/QueryFilterFuncletizationSqlServerTest.cs index 3e033879a3d..ee3ba209e39 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/QueryFilterFuncletizationSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/QueryFilterFuncletizationSqlServerTest.cs @@ -348,17 +348,12 @@ public override void Using_DbSet_in_filter_works() base.Using_DbSet_in_filter_works(); AssertSql( - @"@__ef_filter__Property_0='False' - -SELECT [p].[Id], [p].[Filler] + @"SELECT [p].[Id], [p].[Filler] FROM [PrincipalSetFilter] AS [p] WHERE EXISTS ( SELECT 1 - FROM [Dependents] AS [p0] - WHERE EXISTS ( - SELECT 1 - FROM [MultiContextFilter] AS [e] - WHERE (([e].[IsEnabled] = @__ef_filter__Property_0) AND ([e].[BossId] = 1)) AND ([e].[BossId] = [p0].[PrincipalSetFilterId])) AND ([p0].[PrincipalSetFilterId] = [p].[Id]))"); + FROM [Dependents] AS [d] + WHERE [d].[PrincipalSetFilterId] = [p].[Id])"); } public override void Using_Context_set_method_in_filter_works() @@ -366,14 +361,12 @@ public override void Using_Context_set_method_in_filter_works() base.Using_Context_set_method_in_filter_works(); AssertSql( - @"@__ef_filter__Property_0='False' - -SELECT [p].[Id], [p].[PrincipalSetFilterId] + @"SELECT [p].[Id], [p].[PrincipalSetFilterId] FROM [Dependents] AS [p] WHERE EXISTS ( SELECT 1 - FROM [MultiContextFilter] AS [e] - WHERE (([e].[IsEnabled] = @__ef_filter__Property_0) AND ([e].[BossId] = 1)) AND ([e].[BossId] = [p].[PrincipalSetFilterId]))"); + FROM [MultiContextFilter] AS [b] + WHERE [b].[BossId] = [p].[PrincipalSetFilterId])"); } public override void Static_member_from_dbContext_is_inlined()