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

Query :: errors for some queries with navigations and DefaultIfEmpty #6207

Closed
maumar opened this issue Jul 30, 2016 · 2 comments
Closed

Query :: errors for some queries with navigations and DefaultIfEmpty #6207

maumar opened this issue Jul 30, 2016 · 2 comments
Assignees
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 Jul 30, 2016

Repro:

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

                var l1 = new GameFile { OriginalFilename = "GF1", Filename = "FN1", ContentType = "CT1" };
                var l2 = new GameFile { OriginalFilename = "GF2", Filename = "FN1", ContentType = "CT1" };

                var d1 = new GameDetails { Logo = l1 };
                var d2 = new GameDetails { Logo = l2 };

                var g1 = new Game { Title = "Game1", Details = d1 };
                var g2 = new Game { Title = "Game2", Details = d2 };
                var g3 = new Game { Title = "Game3" };
                var g4 = new Game { Title = "Game4" };

                ctx.Games.AddRange(g1, g2, g3, g4);
                ctx.GameDetails.AddRange(d1, d2);
                ctx.GameFiles.AddRange(l1, l2);
                ctx.SaveChanges();
            }

            using (var ctx = new MyContext())
            {
               // exception during flattening
               var query1 = ctx.Games
                   .Include(g => g.Details)
                   .ThenInclude(d => d.Logo)
                   .DefaultIfEmpty()
                   .Select(g => new { Logo = g.Details.Logo, g.Title, g.GameId })
                   .ToList();

                // invalid SQL
                var query2 = ctx.Games
                    .Include(g => g.Details)
                    .ThenInclude(d => d.Logo)
                    .DefaultIfEmpty()
                    .Select(g => new { Logo = (GameFile)null, g.Title, g.GameId })
                    .ToList();
            }
        }
    }

    public class Game
    {
        public int GameId { get; set; }
        public string Title { get; set; }
        public GameDetails Details { get; set; }
    }

    public class GameDetails
    {
        public int GameDetailsId { get; set; }
        public int GameId { get; set; }
        public GameFile Logo { get; set; }
}

    public class GameFile
    {
        public Guid GameFileId { get; private set; }
        public string ContentType { get; set; }
        public string Filename { get; set; }
        public string OriginalFilename { get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<Game> Games { get; set; }
        public DbSet<GameDetails> GameDetails { get; set; }
        public DbSet<GameFile> GameFiles { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<GameFile>().Property(p => p.Filename).IsRequired(true);
        }

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

First query thows error during compilation (during flattening), second query produces incorrect SQL

Invalid SQL that is produced:

SELECT [t2].[Title], [t2].[GameId]
                FROM (
                    SELECT [t10].*
                    FROM (
                        SELECT NULL AS [empty]
                    ) AS [empty10]
                    LEFT JOIN (
                        SELECT [g0].*
                        FROM [Games] AS [g0]
                    ) AS [t1] ON 1 = 1
                ) AS [t2]
@devseal
Copy link

devseal commented Aug 17, 2016

Hello,
When is this issue going to be resolved?

@maumar maumar assigned smitpatel and unassigned smitpatel Oct 5, 2016
@smitpatel smitpatel assigned smitpatel and unassigned maumar Oct 6, 2016
@smitpatel
Copy link
Contributor

Poaching this. Similar to #6636

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 10, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
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

5 participants