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: Defining query on hierarchy of query types (base and derived) doesn't work #11789

Closed
maumar opened this issue Apr 23, 2018 · 3 comments
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)
        {
            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())
            {
                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; }

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

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Query<LeaderQuery>().ToQuery(() => Set<Leader>().Select(l => new LeaderQuery { Name = l.Name, Faction = l.Faction.Leaders.OrderBy(ll => ll.Id).Select(ll => ll.Faction).FirstOrDefault() } ));
            modelBuilder.Query<LeaderQueryBase>().ToQuery(() => Set<Leader>().Select(l => new LeaderQueryBase { Name = l.Name }));
        }
    }

    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 : LeaderQueryBase
    {
        public Faction Faction { get; set; }
    }

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

this throws:

The query model's result type cannot be changed to 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[QueryTypesTests.LeaderQuery]'. The result type may only be overridden and set to values compatible with the ResultOperators' current data type ('System.Linq.IQueryable`1[QueryTypesTests.LeaderQueryBase]').

   at Remotion.Linq.QueryModel.GetOutputDataInfo()
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.SingleResultToSequence(QueryModel queryModel, Type type) in D:\git\EntityFrameworkCore\src\EFCore\Query\EntityQueryModelVisitor.cs:line 646
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore\Query\EntityQueryModelVisitor.cs:line 177
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel) in D:\git\EntityFrameworkCore\src\EFCore\Storage\Database.cs:line 70

QM:

from Leader l in DbSet<Leader>
select new LeaderQueryBase{ Name = [l].Name }

We seem to be using only the defining query for LeaderQueryBase, when asked to construct DbQuery<LeaderQuery>

@ajcvickers
Copy link
Contributor

Triage: since it is not clear exactly what the behaviors here could be, the plan for 2.1 is to block in model validation any attempt to set a a defining query on a QueryType that is not the base.

@ajcvickers
Copy link
Contributor

This is approved for RTM, but please wait for the branch to open for RTM before merging.

@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 30, 2018
@AndriySvyryd
Copy link
Member

Fixed in 6be065c

@AndriySvyryd AndriySvyryd removed their assignment Apr 30, 2018
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