question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to query all hierarchy levels

See original GitHub issue

Unable to query all hierarchy levels

Steps to reproduce

public class Permission 
    {
        public Permission()
        {
            ChildPermissions = new HashSet<Permission>();
        }

        public int PermissionID { get; set; }
        public string PermissionName { get; set; }
        public int? ParentPermissionID { get; set; }
        public virtual Permission ParentPermission { get; set; }
        public virtual ICollection<Permission> ChildPermissions { get; }
    }
var permissionList = new Permission[] {
                new Permission
                {
                    PermissionID = 1,
                    PermissionName = "1"
                },
                new Permission
                {
                    PermissionID = 2,
                    PermissionName = "1.1",
                    ParentPermissionID = 1
                },
                new Permission
                {
                    PermissionID = 3,
                    PermissionName = "1.2",
                    ParentPermissionID = 1
                },
                new Permission
                {
                    PermissionID = 4,
                    PermissionName = "1.1.1",
                    ParentPermissionID = 2
                },
                new Permission
                {
                    PermissionID = 5,
                    PermissionName = "1.1.2",
                    ParentPermissionID = 2
                }
            };
 _Context.Permission.Include(x => x.ChildPermissions)
                                      .AsEnumerable()
                                      .Where(x => x.ParentPermissionID == null)
                                      .ToList();

The result doesn’t include third level “1.1.1” & “1.1.2” Generated query only shows left join

Further technical details

EF Core version: 3.0 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET Core 3.0) Operating system: IDE: (e.g. Visual Studio 2019 16.3)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ajcvickerscommented, Oct 15, 2019

@NanFengCheong I am not able to reproduce this–see my code below. Please post a small, runnable project/solution or complete code listing, like below, that demonstrates the behavior you are seeing.

public class Permission
{
    public Permission()
    {
        ChildPermissions = new HashSet<Permission>();
    }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int PermissionID { get; set; }
    public string PermissionName { get; set; }
    public int? ParentPermissionID { get; set; }
    public virtual Permission ParentPermission { get; set; }
    public virtual ICollection<Permission> ChildPermissions { get; }
}

public class BloggingContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
        .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Permission>();
    }
}

public class Program
{
    public static void Main()
    {
        using (var context = new BloggingContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var permissionList = new Permission[] {
                new Permission
                {
                    PermissionID = 1,
                    PermissionName = "1"
                },
                new Permission
                {
                    PermissionID = 2,
                    PermissionName = "1.1",
                    ParentPermissionID = 1
                },
                new Permission
                {
                    PermissionID = 3,
                    PermissionName = "1.2",
                    ParentPermissionID = 1
                },
                new Permission
                {
                    PermissionID = 4,
                    PermissionName = "1.1.1",
                    ParentPermissionID = 2
                },
                new Permission
                {
                    PermissionID = 5,
                    PermissionName = "1.1.2",
                    ParentPermissionID = 2
                }
            };
            
            context.AddRange(permissionList);

            context.SaveChanges();
        }

        using (var context = new BloggingContext())
        {
            var results = context.Set<Permission>()
                .Include(x => x.ChildPermissions)
                .AsEnumerable()
                .Where(x => x.ParentPermissionID == null)
                .ToList();
        }
    }
}

image

0reactions
NanFengCheongcommented, Oct 16, 2019

My bad. There is a library turn off following setting causing unexpected behavior.

context.ChangeTracker.AutoDetectChangesEnabled = false;
context.ChangeTracker.LazyLoadingEnabled = false;
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - Query with hierarchical structure and variable number ...
Query with hierarchical structure and variable number of hierarchy levels. Areas are organized in hierarchy up to four levels using PARENT_ID ...
Read more >
Unable to select highest hierarchy date in bar chart visual
Solved: Hi, Has anyone come across the situation where you have a normal bar chart with date heirarchy along X and values up...
Read more >
Can't create a filter on a deep node in hierarchy in BW Query
I'm createing a BW query in a BW/4HANA system, I want to filter by a specific node of a hierarchy, but at certain...
Read more >
Unable to get the idea of hierarchy query
Hi Every oneI'm trying to learn oracle hierarchy query and spent couples of hours but ... select grp, effective_date, termination_date,level.
Read more >
Hierarchical Data Queries: Iteration Limits No Longer ...
When querying hierarchical data, you can use recursive CTEs or the CONNECT BY command to iterate over each level of hierarchy.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found