0

I am trying to query and have the navigational property (the list) return with the correct number of items in it. Currently what I am doing, the list is always empty (Case_Users property in Case object for the example below) when it should not be.

I have these classes:

public class Case
    {
        public Case()
        {
            Create_Date = DateTime.Now;
        }
        [Key]
        public int Id { get; set; }

        [MaxLength(1000)]
        public string CaseNumber { get; set; }

        public ICollection<Case_Users> Case_Users { get; set; } = new List<Case_Users>();
    }
    public class Case_Users
    {
        public Case_Users()
        {
            Create_Date = DateTime.Now;
        }
        [Key]
        public int Id { get; set; }
        [ForeignKey("Case")] 
        public int CaseID { get; set; }
        public Case Case { get; set; }
       
        public string Role {get; set;}
    }

I want to make it so that I can query using the method syntax, starting with the Case and using its navigational property to get the data from its Case_Users navigational property entries, like this:

   var query = _context.Case.Include(c => c.Case_Users).ToList();

I am getting simply my list object returned, but the Case_Users list object is empty. Even though it should not be.

Is it possible to query based on the navigational property, and if so what is the syntax?

user9758771
  • 109
  • 1
  • 8
  • 1
    Does this answer your question? [Filtering on Include in EF Core](https://stackoverflow.com/questions/43618096/filtering-on-include-in-ef-core) – Dylan Dec 16 '21 at 21:58
  • One issue is that you're not awaiting the `ToListAsync` – juharr Dec 16 '21 at 21:59
  • I fixed the async issue, that was an error in me transferring the question. That question doesnt answer my issue because I am in EF core 3 and also I just cannot get the list to return anything at all other than an empty list, the filtering was just an extra piece I added on and have now removed – user9758771 Dec 17 '21 at 00:14

0 Answers0