填充子屬性給出以下POCO代碼第一次實體與實體框架類SqlQuery
public class Customer
{
public int CustomerId { get; set; }
public string CustomerTitle { get; set; }
public string CustomerFirstName { get; set; }
public string CustomerLastName { get; set; }
public ICollection<Order> Orders { get; set; }
}
public class Order
{
public int OrderId { get; set; }
...
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
使用LINQ,你可以得到通過包括物業填寫訂單在
var cust = (from cust in context.Customer
where cust.CustomerId == 1
select cust)
.Include(ord => ord.Orders)
.FirstOrDefault();
我想以獲得相同的結果使用paramaterised SQL,使用
Customer co = context.Customer.SqlQuery(
@"select [Customer].[CustomerId],
...
[Order].[OrderId] AS [OrderId],
...
from Customer join Order on Customer.CustomerId = Order.CustomerId where Customer.CustomerId = @custid", sqlParm)
.FirstOrDefault();
我該如何ge t co.Orders中的Orders用上面的命令填充,似乎我不能在Include語句中使用SqlQuery。這只是一個非常簡單的例子,僅供說明之用,實際查詢將更多涉及。
FirstOrDefault()導致查詢被執行,所以這實際上等於執行兩個查詢 –