Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasQueryFilter ignores ToQuery declaration #15263

Closed
zamzowd opened this issue Apr 5, 2019 · 1 comment
Closed

HasQueryFilter ignores ToQuery declaration #15263

zamzowd opened this issue Apr 5, 2019 · 1 comment

Comments

@zamzowd
Copy link

zamzowd commented Apr 5, 2019

If a DbQuery is set up with ToQuery, and that DbQuery is used in HasQueryFilter for another DbSet or DbQuery, Entity Framework tries to query a saved view rather than use the ToQuery definition.

DataContext:

public DbSet<Foo> Foos { get; set; }
public DbQuery<FilteredFoo> FilteredFoos { get; set; }
public DbSet<Bar> Bars { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
  builder.Query<FilteredFoo>().ToQuery(() => 
    Foos
      .Where(b => !b.IsDeleted)
      .Select(foo => new FilteredFoo { Id = foo.Id, BarId = foo.BarId })
  );
  builder.Entity<Bar>().HasQueryFilter(bar => FilteredFoos.Any(foo => foo.BarId == bar.Id));
}
var foos = await Context.FilteredFoos.ToListAsync();
var bars = await Context.Bars.ToListAsync();

The first call to FilteredFoos works, but the second call to Bars fails with: SqlException: Invalid object name 'FilteredFoos'. The constructed SQL for the call to Bars has WHERE EXISTS (SELECT 1 FROM FilteredFoos) rather than inject the Linq-To-Sql from ToQuery.

@ajcvickers
Copy link
Contributor

Duplicate of #13361

@ajcvickers ajcvickers marked this as a duplicate of #13361 Apr 8, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants