Skip to content

Commit

Permalink
Merge pull request #18770 from aspnet/ObjectIdentifiedAsContext3.1
Browse files Browse the repository at this point in the history
Exclude Object when checking for DbContext in query filters
  • Loading branch information
wtgodbe authored Nov 11, 2019
2 parents 3442750 + 23ac7d2 commit 53779f7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,10 @@ public ContextParameterReplacingExpressionVisitor(Type contextType)
public ParameterExpression ContextParameterExpression { get; }

public override Expression Visit(Expression expression)
{
return expression?.Type.GetTypeInfo().IsAssignableFrom(_contextType) == true
=> expression?.Type != typeof(object)
&& expression?.Type.GetTypeInfo().IsAssignableFrom(_contextType) == true
? ContextParameterExpression
: base.Visit(expression);
}
}

private static Expression RemoveConvert(Expression expression)
Expand Down
51 changes: 51 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7361,6 +7361,57 @@ public BugContext18087(DbContextOptions options)

#endregion

#region Issue18759

[ConditionalFact]
public void Query_filter_with_null_constant()
{
using var _ = CreateDatabase18759();
using var context = new BugContext18759(_options);

var people = context.People.ToList();

AssertSql(
@"SELECT [p].[Id], [p].[UserDeleteId]
FROM [People] AS [p]
LEFT JOIN [User18759] AS [u] ON [p].[UserDeleteId] = [u].[Id]
WHERE [u].[Id] IS NOT NULL");
}

private SqlServerTestStore CreateDatabase18759()
=> CreateTestStore(
() => new BugContext18759(_options),
context =>
{
ClearLog();
});

public class Person18759
{
public int Id { get; set; }
public User18759 UserDelete { get; set; }
}

public class User18759
{
public int Id { get; set; }
}

private class BugContext18759 : DbContext
{
public DbSet<Person18759> People { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Person18759>().HasQueryFilter(p => p.UserDelete != null);

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

#endregion Issue18759

private DbContextOptions _options;

private SqlServerTestStore CreateTestStore<TContext>(
Expand Down

0 comments on commit 53779f7

Please sign in to comment.