Populating child property with Entity Framework SqlQuery

This is not possible at all. Direct SQL execution doesn't offer filling of navigation properties and you really can't use Include. You must execute two separate SQL queries to get Cutomer and her Orders.


I have used the following class structure as workaround:

public class Customer
{
    public int CustomerId { get; set; }
}

public class Order
{
    public Customer Customer { get; set; }
    private int _customerId;
    private int CustomerId { get { return _customerId; } set { Customer.CustomerId = _customerId = value; } }
    public Order()
    {
        Customer = new Customer();
    }
}

In this case you don't need to run the query twice, and the following query would give the order with customer:

db.Database.SqlQuery<Order>(
    "select cu.CustomerId, ord.OrderId from Orders ord join Customer cu on cu.CustomerId=ord.CustomerId")
    .ToList();