Skip to content

Query: accessing property on a required navigation defined on a derived type produces INNER JOIN #10988

Closed

Description

@maumar

Repro:

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

                var school = new School { Name = "S1" };

                var p1 = new Parent { ParentName = "p1" };
                var p2 = new Parent { ParentName = "p2" };
                var s1 = new Student { StudentName = "p1", School = school };
                var s2 = new Student { StudentName = "p1", School = school };

                ctx.Schools.Add(school);
                ctx.People.AddRange(p1, p2, s1, s2);
                ctx.SaveChanges();
            }

            using (var ctx = new MyContext())
            {
                Console.WriteLine(ctx.People.Count()); // 4

                var query = ctx.People.OrderBy(p => ((Student)p).School.Name);
                var result = query.ToList();

                Console.WriteLine(result.Count); // 2
            }
        }
    }

    public class MyContext : DbContext
    {
        public DbSet<Person> People { get; set; }
        public DbSet<School> Schools { get; set; }

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

    public class Person
    {
        public int Id { get; set; }
    }

    public class Student : Person
    {
        public string StudentName { get; set; }

        public School School { get; set; }
        public int SchoolId { get; set; }
    }

    public class Parent : Person
    {
        public string ParentName { get; set; }
    }

    public class School
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public List<Student> Students { get; set; }
    }

We generate the following query:

SELECT [p].[Id], [p].[Discriminator], [p].[SchoolId], [p].[StudentName]
FROM [People] AS [p]
INNER JOIN [Schools] AS [p.School] ON CASE
    WHEN [p].[Discriminator] = N'Student'
    THEN [p].[SchoolId] ELSE NULL
END = [p.School].[Id]
WHERE [p].[Discriminator] IN (N'Student', N'Person')
ORDER BY [p.School].[Name]

We should be producing LOJ here, in case not all people are students (such query would throw cast exception in L2O, but we allow it to execute)

Activity

  1. self-assigned this
    on Feb 16, 2018
  2. heightpi commented on Feb 16, 2018

    @heightpi

    Until this will be fixed, i used nullable int to generate a LEFT JOIN
    public int? SchoolId { get; set; } in the Student Class

  3. changed the title [-]Query: order by property on a required navigation defined on a derived type produces INNER JOIN[/-] [+]Query: accessing property on a required navigation defined on a derived type produces INNER JOIN[/+] on Feb 16, 2018
  4. added this to the 2.1.0 milestone on Feb 16, 2018
  5. ajcvickers commented on Feb 17, 2018

    @ajcvickers
    Contributor

    Decision from triage: we should make both the explicit cast and the as cast work in the same way, including null compensation/propagation.

  6. added 3 commits that reference this issue on Feb 21, 2018
  7. maumar commented on Feb 21, 2018

    @maumar
    ContributorAuthor

    Fixed in 1fcdbea

  8. iudelsmann commented on Mar 2, 2018

    @iudelsmann

    This is not included in 2.1.0-preview1 but seems important to me. I was happy to have the fix for #3910 but including my derived properties is causing INNER JOIN thus not solving my problem (I'm currently explicitly loading those properties). Is it planned to add this to 2.1?

  9. heightpi commented on Mar 2, 2018

    @heightpi

    @iudelsmann as I said in a comment before, changing from int to int? solved my problem, and generates a left join, without the need to explicitly load the properties

  10. iudelsmann commented on Mar 2, 2018

    @iudelsmann

    @heightpi It does work indeed, but it would also allow someone to save a null value where I wouldn't want one. I can't have the restriction in the database since the parent class doesn't have the property, so I must have the restriction in my code. When someone tries to save value zero the database will throw an error since it violates the foreign key constraint.

    I could do this or another workaround, but I prefer waiting for the release since this is already fixed.

  11. smitpatel commented on Mar 2, 2018

    @smitpatel
    Contributor

    @iudelsmann - This is fixed in 2.1.0 milestone so it will be included in RTM release of 2.1.0. It should be included in next release (preview/rc/RTM). The milestone will change accordingly to indicate the exact release number.

  12. ajcvickers commented on Mar 2, 2018

    @ajcvickers
    Contributor
  13. iudelsmann commented on Mar 13, 2018

    @iudelsmann

    @ajcvickers Sorry for the delay, had some busy days and some troubles. Tried to test this now but seems to be getting a problem with missing apostrophes surrounding my discriminator values, causing the query to be invalid throwing an exception in postgres. Using ef core 2.1.0-preview2-30301 and npgsql.PostgreSQL 2.1.0-ci-00132.

  14. ajcvickers commented on Mar 13, 2018

    @ajcvickers
    Contributor

    @iudelsmann Can you file a new issue for this including a runnable project/solution or complete code listing so that we can investigate? /cc @roji

  15. roji commented on Mar 13, 2018

    @roji
    Member

    Any missing apostrophes sound more like an issue in the Npgsql provider (and unrelated to the original issue above), can you please post a code sample on Npgsql.EntityFrameworkCore.PostgreSQL?

  16. modified the milestones: , 2.1.0 on Nov 11, 2019
  17. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions