Let's say I have a Dependents table, like this
Id | Name | EmployeeId
1 | Luke | 1
2 | Leia | 1
3 | Tim | 4
And then I have an Employees table, like this
Id | EmployeeName | JobTitle
1 | Anakin | Enforcer
2 | Tarkin | Senior Vice President
3 | Palpatine | CEO
For dependents, EmployeeId is a foreign key connecting them to the Employees table. Finally, I have an EmployeeDbo class, like this:
public class EmployeeDbo
{
public int Id { get; set; }
public string EmployeeName { get; set; }
public string JobTitle { get; set; }
public IEnumerable<DependentDbo> Dependents { get; set; }
}
So as you can see, the EmployeeDbo includes the dependents as a collection. Using LINQ and DbContext, getting all employees with a populated list would just be var results = _dbContext.Employees.Include(x => x.Dependents).ToList()
Here's my problem. I'm using a stored procedure to select the data I want. The actual data is much more complicated, but here's what my code calling the stored proc looks like:
var employees =
_dbContext.Database.SqlQuery<EmployeeDbo>(@"[GetEmployees] @someParameter", sqlParam);
This returns an object of type DbRawSqlQuery<EmployeeDbo>
. When I call ToList()
the actual SQL call is performed, but it does NOT populate my dbo's IEnumerable<Dependent>
collection, it just comes back as null. I can't do Include()
on the DbRawSqlQuery.
What are my options for populating my Dbo's Dependent collection with this call to the stored proc?