Share via


方法: 遅延読み込みを使用して関連するオブジェクトを読み込む (Entity Framework)

このトピックでは、遅延読み込みを使用して関連するオブジェクトを読み込む方法について説明します。 遅延読み込みが有効になっている場合、ナビゲーション プロパティを使用して関連オブジェクトにアクセスした時点で、それらのオブジェクトが読み込まれます。 オブジェクトは Include メソッドを使用して集中的に読み込むことも、LoadProperty メソッドを使用して明示的に読み込むこともできます。 詳細については、「関連オブジェクトの読み込み (Entity Framework)」を参照してください。

Entity Framework ランタイムで、ObjectContext のインスタンスの LazyLoadingEnabled プロパティの既定値は、false です。 ただし、Entity Framework ツールを使用して新しいモデルおよび対応する生成済みクラスを作成する場合、生成されたコードによって、生成済みオブジェクト コンテキストのコンストラクターにおける LazyLoadingEnabledtrue に設定されます。

このトピックの例には、Adventure Works Sales Model が使用されています。このトピックのコードを実行するには、あらかじめプロジェクトに Adventure Works Sales Model を追加し、Entity Framework を使用するようにプロジェクトを構成しておく必要があります。詳細については、「Entity Data Model ウィザードを使用する方法 (Entity Framework)」、または「Entity Framework プロジェクトを手動で構成する方法」、および「Entity Data Model を手動で定義する方法 (Entity Framework)」を参照してください。

次の例では 10 の連絡先を表示し、ユーザーに 1 つ選択するように求めます。 選択された連絡先に基づいて、関連付けられている注文が読み込まれます。

Class LazyLoading
    Public Sub EnableLazyLoading()
        Using context As New AdventureWorksEntities()
            ' You do not have to set context.ContextOptions.LazyLoadingEnabled to true 
            ' if you used the Entity Framework to generate the object layer. 
            ' The generated object context type sets lazy loading to true 
            ' in the constructor. 
            context.ContextOptions.LazyLoadingEnabled = True

            ' Display ten contacts and select a contact 
            Dim contacts = context.Contacts.Take(10)
            For Each c In contacts
                Console.WriteLine(c.ContactID)
            Next

            Console.WriteLine("Select a customer:")
            Dim contactID As Int32 = Convert.ToInt32(Console.ReadLine())

            ' Get a specified customer by contact ID. 
            Dim contact = context.Contacts.Where(Function(c) c.ContactID = contactID).FirstOrDefault()

            ' If lazy loading was not enabled no SalesOrderHeaders would be loaded for the contact. 
            For Each order As SalesOrderHeader In contact.SalesOrderHeaders
                Console.WriteLine("SalesOrderID: {0} Order Date: {1} ", order.SalesOrderID, order.OrderDate)
            Next
        End Using
    End Sub
End Class
class LazyLoading
{
    public void EnableLazyLoading()
    {
        using (AdventureWorksEntities context =
            new AdventureWorksEntities())
        {
            // You do not have to set context.ContextOptions.LazyLoadingEnabled to true 
            // if you used the Entity Framework to generate the object layer.
            // The generated object context type sets lazy loading to true
            // in the constructor. 
            context.ContextOptions.LazyLoadingEnabled = true;

            // Display ten contacts and select a contact
            var contacts = context.Contacts.Take(10);
            foreach (var c in contacts)
                Console.WriteLine(c.ContactID);

            Console.WriteLine("Select a customer:");
            Int32 contactID = Convert.ToInt32(Console.ReadLine());

            // Get a specified customer by contact ID. 
            var contact = context.Contacts.Where(c => c.ContactID == contactID).FirstOrDefault();

            // If lazy loading was not enabled no SalesOrderHeaders would be loaded for the contact.
            foreach (SalesOrderHeader order in contact.SalesOrderHeaders)
            {
                Console.WriteLine("SalesOrderID: {0} Order Date: {1} ",
                    order.SalesOrderID, order.OrderDate);
            }
        }
    }
}

参照

処理手順

方法: クエリ パスを使用して結果を構築する (Entity Framework)
方法: 関連するオブジェクトを明示的に読み込む (Entity Framework)

概念

関連オブジェクトの読み込み (Entity Framework)
関連 POCO エンティティの読み込み (Entity Framework)