Skip to content

Commit

Permalink
Revert "Fix #11803 - QueryFilters: query filters are not applied recu…
Browse files Browse the repository at this point in the history
…rsively"

This reverts commit 4a35f9d.

Resolves #13346
  • Loading branch information
smitpatel committed Sep 20, 2018
1 parent fecef9c commit 942adc4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Query/RelationalQueryModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
/// <returns>true if the target type should have a defining query applied.</returns>
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<FromSqlResultOperator>()
.All(a => a.QuerySource != querySource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -143,7 +143,7 @@ var predicateExpression
.Replace(
oldParameterExpression,
newParameterExpression,
Visit(parameterizedFilter.Body));
parameterizedFilter.Body);

var whereExpression
= Expression.Call(
Expand Down
70 changes: 68 additions & 2 deletions test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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<OrderSummary13346>().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<Order13346> Orders { get; set; }

public MyContext13346(DbContextOptions options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Query<OrderSummary13346>().ToQuery(
() => Query<OrderSummary13346>()
.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,32 +348,25 @@ 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()
{
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()
Expand Down

0 comments on commit 942adc4

Please sign in to comment.