方法: POCO エンティティを明示的に読み込む (Entity Framework)

このトピックの例では、ObjectContextLoadProperty メソッドを使用して、関連オブジェクトを明示的に読み込む方法を示します。 関連オブジェクトにアクセスするために遅延読み込みを使用する場合の例については、「方法: 遅延読み込みを使用して関連するオブジェクトを読み込む (Entity Framework)」を参照してください。 LoadProperty メソッドは、POCO エンティティ、および EntityObject から派生するエンティティと共に使用できます。

このトピックの例では、「方法: POCO エンティティを定義する (Entity Framework)」で定義されている POCO データ クラス、「方法: カスタム オブジェクト コンテキストを定義する (Entity Framework)」で作成された AdventureWorksEntities クラス (ObjectContext の派生クラス)、および「カスタム オブジェクトを操作できるようにモデリング ファイルとマッピング ファイルをカスタマイズする方法 (Entity Framework)」で定義されている AdventureWorks ベースのデータ モデルを使用します。

この例は、最初の 5 つの Order オブジェクトを返し、LoadProperty メソッドを呼び出して、Order ごとに関連する LineItem オブジェクトを明示的に読み込みます。

Using context As New POCOAdventureWorksEntities()
    Try
        ' Disable lazy loading. 
        context.ContextOptions.LazyLoadingEnabled = False

        ' Get the first five orders. 
        For Each order As Order In context.Orders.Take(5)
            ' Because LazyLoadingEnabled is set to false, 
            ' we need to explicitly load the related line items for the order. 
            context.LoadProperty(order, "LineItems")

            Console.WriteLine(String.Format("PO Number: {0}", order.ExtendedInfo.PurchaseOrderNumber))
            Console.WriteLine(String.Format("Order Date: {0}", order.OrderDate.ToString()))
            Console.WriteLine("Order items:")
            For Each item As LineItem In order.LineItems
                Console.WriteLine(String.Format("Product: {0} " & "Quantity: {1}", item.ProductID, item.OrderQty))
            Next
        Next
    Catch ex As InvalidOperationException
        Console.WriteLine(ex.Message)
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
End Using
using (POCOAdventureWorksEntities context =
        new POCOAdventureWorksEntities())
{
    try
    {
        // Disable lazy loading.
        context.ContextOptions.LazyLoadingEnabled = false;

        // Get the first five orders.
        foreach (Order order in context.Orders.Take(5))
        {
            // Because LazyLoadingEnabled is set to false,
            // we need to explicitly load the related line items for the order.
            context.LoadProperty(order, "LineItems");

            Console.WriteLine(String.Format("PO Number: {0}",
                order.ExtendedInfo.PurchaseOrderNumber));
            Console.WriteLine(String.Format("Order Date: {0}",
                order.OrderDate.ToString()));
            Console.WriteLine("Order items:");
            foreach (LineItem item in order.LineItems)
            {
                Console.WriteLine(String.Format("Product: {0} "
                    + "Quantity: {1}", item.ProductID,
                    item.OrderQty));
            }
        }
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

参照

概念

関連 POCO エンティティの読み込み (Entity Framework)
オブジェクトのカスタマイズ (Entity Framework)
POCO エンティティの使用 (Entity Framework)