Reputation: 1500
I have a database Model that looks like this:
My Class for each looks as below:
class Complaints{
//props
public virtual ICollection<MONITORING> MONITORINGs { get; set; }
}
class Monitoring{
//props
public virtual ICollection<MonitoringNotes> MONITORINGNotes { get; set; }
}
class MonitoringNotes{
//props
}
I get all my data when I run the repository to get all as bellow
public IEnumerable<Complaints> GetAll()
{
try
{
return _context.Complaints
.Include(t => t.MONITORINGs)
.OrderBy(t => t.FileNum)
.ToList();
}
catch (Exception ex)
{
_logger.LogError("Could not get complaint with checklist", ex);
return null;
}
}
but when I add the notes to the monitoring with the select, it returns Null:
public IEnumerable<Complaints> GetAll()
{
try
{
return _context.Complaints
.Include(t => t.MONITORINGs.Select(t3=>t3.MONITORINGNotes )
.OrderBy(t => t.FileNum)
.ToList();
}
catch (Exception ex)
{
_logger.LogError("Could not get complaint with checklist", ex);
return null;
}
}
Also when I type in select, I get the Intellisense for the notes, so I don't think its my entities. Can someone point me into the right direction if I am using the select statement correctly? I used this solution on many of the questions asked about including the level 3 relationships, like this one: Entity Framework - Include Multiple Levels of Properties.
Upvotes: 1
Views: 423
Reputation: 1500
Using then include solved my problem.
public IEnumerable<Complaints> GetAll()
{
try
{
return _context.Complaints
.Include(t => t.MONITORINGs)
.ThenInclude(t3=>t3.MONITORINGNotes )
.OrderBy(t => t.FileNum)
.ToList();
}
catch (Exception ex)
{
_logger.LogError("Could not get complaint with checklist", ex);
return null;
}
}
Upvotes: 1