1
votes

I have the entities below, and the query at the bottom. Im not following how to include the Hearing's child entity collection (Digital Content) in the return set. Its blank (when it shouldnt be) if I dont use includes, and using includes I get the following error mesage:

$exception {"The expression 'h.digital_Contents' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393."} System.InvalidOperationException

public class Hearing
{
    [Key]
    public int Id { get; set; }

    public int HearingNumber { get; set; }

    public DateTime? End_Date { get; set; }

    public ICollection<Digital_Content> digital_Contents = new List<Digital_Content>();

}

public class Digital_Content
{
    [Key]
    public int Id { get; set; }

    [ForeignKey("Hearing")]
    public int HearingId { get; set; }

    public Hearing Hearing { get; set; }

    public string BlobStorageLink { get; set; }

    public DateTime? End_Date { get; set; }
}

Query:

 var ListHearings = await _context.Hearing
        .Where(h => h.HearingNumber == 55)
        .Include(h => h.digital_Contents)
        .ToListAsync();     
     
1

1 Answers

3
votes

Add a { get; set; } to your digital_contents to change it from a field to a property so your digital_contents can to be read and written to (i.e. populate your property):

public ICollection<Digital_Content> digital_Contents { get; set; } = new List<Digital_Content>();

Then your .Include(x => x.digital_Contents) should populate in your return set.