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

QueryFilters: query filters are not applied recursively #11803

Closed
maumar opened this issue Apr 24, 2018 · 1 comment
Closed

QueryFilters: query filters are not applied recursively #11803

maumar opened this issue Apr 24, 2018 · 1 comment

Comments

@maumar
Copy link
Contributor

maumar commented Apr 24, 2018

Similar to #11791

If query filter uses DbSet for which another query filter is defined, that second query filter is ignored.

repro:

    class Program
    {
        public static int Count { get; set; }

        static void Main(string[] args)
        {
            using (var ctx = new MyContext())
            {
                ctx.Database.EnsureDeleted();
                ctx.Database.EnsureCreated();

                var f1 = new Faction { Name = "Skeliege" };
                var f2 = new Faction { Name = "Monsters" };
                var f3 = new Faction { Name = "Nilfgaard" };
                var f4 = new Faction { Name = "Northern Realms" };
                var f5 = new Faction { Name = "Scioia'tael" };

                var l11 = new Leader { Faction = f1, Name = "Bran Tuirseach" };
                var l12 = new Leader { Faction = f1, Name = "Crach an Craite" };
                var l13 = new Leader { Faction = f1, Name = "Eist Tuirseach" };
                var l14 = new Leader { Faction = f1, Name = "Harald the Cripple" };

                ctx.Factions.AddRange(f1, f2, f3, f4, f5);
                ctx.Leaders.AddRange(l11, l12, l13, l14);
                ctx.SaveChanges();
            }

            using (var ctx = new MyContext())
            {
                var q = ctx.Factions.ToList();
            }
        }
    }

    public class MyContext : DbContext
    {
        public DbSet<Faction> Factions { get; set; }
        public DbSet<Leader> Leaders { get; set; }

        public DbQuery<LeaderQuery> LeadersQuery { get; set; }
        public DbQuery<FactionQuery> FactionsQuery { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
         optionsBuilder.UseSqlServer(@"Server=.;Database=QueryTypesRepro;Trusted_Connection=True;MultipleActiveResultSets=True");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Leader>().HasQueryFilter(l => l.Name.StartsWith("Bran")); // this one is ignored
            modelBuilder.Entity<Faction>().HasQueryFilter(f => Leaders.Any(l => l.Name == "Crach an Craite"));
        }
    }

    public class Faction
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public List<Leader> Leaders { get; set; }
    }

    public class Leader
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Faction Faction { get; set; }
    }

we produce the following query plan:

from Faction f in DbSet<Faction>
where 
    (from Leader l in DbSet<Leader>
    where [l].Name == "Crach an Craite"
    select [l]).Any()
select [f]
@ajcvickers ajcvickers added this to the 2.2.0 milestone Apr 25, 2018
anpete added a commit that referenced this issue May 2, 2018
- Visit generated subqueries/predicates to ensure nested filters get applied.
anpete added a commit that referenced this issue May 2, 2018
- Visit generated subqueries/predicates to ensure nested filters get applied.
anpete added a commit that referenced this issue May 2, 2018
- Visit generated subqueries/predicates to ensure nested filters get applied.
@anpete anpete closed this as completed in 4a35f9d May 3, 2018
@anpete anpete added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 3, 2018
@smitpatel smitpatel removed the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 18, 2018
@smitpatel smitpatel removed this from the 2.2.0-preview2 milestone Sep 18, 2018
@smitpatel
Copy link
Member

The fix for this issue is being reverted due to #13346 and will be tracked at #13361

smitpatel added a commit that referenced this issue Sep 19, 2018
smitpatel added a commit that referenced this issue Sep 19, 2018
smitpatel added a commit that referenced this issue Sep 20, 2018
@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

4 participants