HOW TO:明確載入相關的物件 (Entity Framework)
本主題提供的範例將說明如何明確載入相關的物件。第一個範例會明確載入單一客戶的所有訂單和項目。第二個範例會使用個別的查詢,只載入選取的訂單與相關項目。接著將這些訂單附加給客戶。
本主題的範例是根據 Adventure Works Sales Model。若要執行此主題中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 實體架構。若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案和 HOW TO:以手動方式定義 Entity Data Model (Entity Framework) 中的程序。您也可以使用 [Entity Data Model 精靈] 定義 AdventureWorks Sales Model。如需詳細資訊,請參閱 HOW TO:使用 Entity Data Model 精靈 (Entity Framework)。
範例
下列範例會載入屬於單一 Contact 的SalesOrderHeader 物件,然後再逐一查看 EntityCollection 中的 SalesOrderHeader 物件。對於集合中的每個 SalesOrderHeader 物件,會呼叫 Load 方法以從資料庫擷取相關 SalesOrderDetail 物件的集合。
' Specify the customer ID.
Dim customerId As Integer = 4332
Using context As New AdventureWorksEntities
Try
' Get a specified customer by contact ID.
Dim customer As Contact = context.Contact _
.Where("it.ContactID = @customerId", _
New ObjectParameter("customerId", customerId)).First()
' Load the orders for the customer
If (Not customer.SalesOrderHeader.IsLoaded) Then
customer.SalesOrderHeader.Load()
End If
For Each order As SalesOrderHeader In customer.SalesOrderHeader
' Load the items for the order if not already loaded.
If Not order.SalesOrderDetail.IsLoaded Then
order.SalesOrderDetail.Load()
End If
Console.WriteLine(String.Format("PO Number: {0}", _
order.PurchaseOrderNumber))
Console.WriteLine(String.Format("Order Date: {0}", _
order.OrderDate.ToString()))
Console.WriteLine("Order items:")
Dim item As SalesOrderDetail
For Each item In order.SalesOrderDetail
Console.WriteLine(String.Format("Product:{0}" _
+ "Quantity: {1}", item.ProductID.ToString(), _
item.OrderQty.ToString()))
Next
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
Catch ex As EntityCommandExecutionException
Console.WriteLine(ex.ToString())
End Try
End Using
// Specify the customer ID.
int customerId = 4332;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Get a specified customer by contact ID.
Contact customer = context.Contact
.Where("it.ContactID = @customerId",
new ObjectParameter("customerId", customerId)).First();
// Load the orders for the customer
if (!customer.SalesOrderHeader.IsLoaded)
{
customer.SalesOrderHeader.Load();
}
foreach (SalesOrderHeader order in customer.SalesOrderHeader)
{
// Load the items for the order if not already loaded.
if (!order.SalesOrderDetail.IsLoaded)
{
order.SalesOrderDetail.Load();
}
Console.WriteLine(String.Format("PO Number: {0}",
order.PurchaseOrderNumber));
Console.WriteLine(String.Format("Order Date: {0}",
order.OrderDate.ToString()));
Console.WriteLine("Order items:");
foreach (SalesOrderDetail item in order.SalesOrderDetail)
{
Console.WriteLine(String.Format("Product: {0} "
+ "Quantity: {1}", item.ProductID.ToString(),
item.OrderQty.ToString()));
}
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
catch (EntityCommandExecutionException ex)
{
Console.WriteLine(ex.ToString());
}
}
下列範例會對 EntityCollection 使用 CreateSourceQuery 方法,只載入屬於單一 Contact 的 5 個 SalesOrderHeader 物件與相關 SalesOrderDetail 物件。接著會將這個查詢結果附加到原始 SalesOrderHeaderEntityCollection 中。
' Specify the customer ID.
Dim customerId As Integer = 4332
Using context As New AdventureWorksEntities
Try
' Get a specified customer by contact ID.
Dim customer As Contact = context.Contact _
.Where("it.ContactID = @customerId", _
New ObjectParameter("customerId", customerId)).First()
' Return the customer's first five orders with line items and
' attach them to the SalesOrderHeader collection.
customer.SalesOrderHeader.Attach( _
customer.SalesOrderHeader.CreateSourceQuery() _
.Include("SalesOrderDetail").Take(5))
For Each order As SalesOrderHeader In customer.SalesOrderHeader
Console.WriteLine(String.Format("PO Number: {0}", _
order.PurchaseOrderNumber))
Console.WriteLine(String.Format("Order Date: {0}", _
order.OrderDate.ToString()))
Console.WriteLine("Order items:")
For Each item As SalesOrderDetail In order.SalesOrderDetail
Console.WriteLine(String.Format("Product: {0} " _
+ "Quantity: {1}", item.ProductID.ToString(), _
item.OrderQty.ToString()))
Next
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
// Specify the customer ID.
int customerId = 4332;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Get a specified customer by contact ID.
Contact customer = context.Contact
.Where("it.ContactID = @customerId",
new ObjectParameter("customerId", customerId)).First();
// Return the customer's first five orders with line items and
// attach them to the SalesOrderHeader collection.
customer.SalesOrderHeader.Attach(
customer.SalesOrderHeader.CreateSourceQuery()
.Include("SalesOrderDetail").Take(5));
foreach (SalesOrderHeader order in customer.SalesOrderHeader)
{
Console.WriteLine(String.Format("PO Number: {0}",
order.PurchaseOrderNumber));
Console.WriteLine(String.Format("Order Date: {0}",
order.OrderDate.ToString()));
Console.WriteLine("Order items:");
foreach (SalesOrderDetail item in order.SalesOrderDetail)
{
Console.WriteLine(String.Format("Product: {0} "
+ "Quantity: {1}", item.ProductID.ToString(),
item.OrderQty.ToString()));
}
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
另請參閱
工作
HOW TO:執行傳回實體類型的查詢 (Entity Framework)
HOW TO:使用查詢路徑來設定結果外觀 (Entity Framework)
HOW TO:使用導覽屬性巡覽關聯性 (Entity Framework)