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

QueryTypes: using DbQuery inside a defining query of another QueryType produces incorrect SQL - treating the referenced DbQuery as if it were a DbSet #11791

Closed
maumar opened this issue Apr 23, 2018 · 1 comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Apr 23, 2018

Repro:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            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 q02 = ctx.LeadersQuery.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.Query<FactionQuery>().ToQuery(() => Set<Leader>().Where(lq => lq.Name != "Foo").Select(lq => new FactionQuery { Name = "Not Foo" }));
            modelBuilder.Query<LeaderQuery>().ToQuery(() => Query<FactionQuery>().Where(fq => fq.Name != "Bar").Select(fq => new LeaderQuery { Name = "Not Bar" }));
        }
    }

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

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

    public class FactionQuery
    {
        public string Name { get; set; }
    }

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

    public class LeaderQuery
    {
        public string Name { get; set; }
    }

we produce the following SQL:

SELECT 1
            FROM [FactionsQuery] AS [fq]
            WHERE ([fq].[Name] <> N'Bar') OR [fq].[Name] IS NULL

which is incorrect because there is no FactionsQuery table

@maumar
Copy link
Contributor Author

maumar commented Apr 23, 2018

if we enable this, we need to watch out for cycles

@ajcvickers ajcvickers added this to the 2.2.0 milestone Apr 24, 2018
anpete added a commit that referenced this issue May 3, 2018
anpete added a commit that referenced this issue May 3, 2018
anpete added a commit that referenced this issue May 3, 2018
anpete added a commit that referenced this issue May 3, 2018
@anpete anpete closed this as completed 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
@ajcvickers ajcvickers modified the milestones: 2.2.0-preview2, 2.2.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants