次の方法で共有


複合型を使用してオブジェクト クエリを作成および実行する方法 (Entity Framework)

この例では、「複合型を使用してモデルを定義する方法 (Entity Framework)」のトピックで定義されたスキーマを使用します。

複合型を使用してプロジェクトを作成するには

  1. CustomerComplexAddrClient という名前のコンソール アプリケーション プロジェクトを作成し、System.Data.Entity および System.Runtime.Serialization への参照を追加します。

  2. 複合型を使用してモデルを定義する方法 (Entity Framework)」のトピックに説明されているプロジェクトからビルドされた DLL への参照を追加します。

  3. トピック「複合型を使用してモデルを定義する方法 (Entity Framework)」のスキーマを実行可能ファイルと同じフォルダに追加します。

  4. 例に従ってアプリケーション構成ファイルを作成します。

  5. 例に示したコードを Program.cs ファイルにコピーします。

  6. 次に示す内容のアプリケーション構成ファイルを追加します。

  7. プロジェクトをビルドして実行します。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="CustomerComplexAddressContext" 
         connectionString="metadata=.;
         provider=System.Data.SqlClient;
         provider connection string=&quot;
         Data Source=serverName;
         Initial Catalog=CustomerWComplexAddr;
         Integrated Security=True;
         multipleactiveresultsets=true&quot;"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

このコード例では、すべての CCustomers と、CAddress 複合型の内部プロパティを表示します。オブジェクト コンテキストには、複合プロパティ Address を持つ CCustomers のコレクションが含まれています。Address プロパティの型は、CAddress です。そのすべての内部プロパティには、CCustomer 型のインスタンスからアクセスできます。

Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports CustomerComplexAddress_VB
Imports CustomerComplexAddress_VB.CustomerComplexAddr

Module Module1
    Sub Main()
        Try
            Using objCtx As CustomerComplexAddrContext = _
                    New CustomerComplexAddrContext()
                For Each customer As CCustomer In objCtx.CCustomers
                    Console.WriteLine("Customer Id: " & _
                        "{0} {1}" & vbNewLine & "{2} {3}," & _
                        "{4} {5}" & vbNewLine & "Phone: {6}", _
                        customer.CustomerId.ToString(), _
                        customer.CompanyName, _
                        customer.Address.StreetAddress, _
                        customer.Address.City, _
                        customer.Address.Region, _
                        customer.Address.PostalCode, _
                        customer.Address.Phone)
                    Console.Write(vbNewLine)
                Next
            End Using
        Catch ex As Exception
            Console.WriteLine(ex.ToString())
        End Try
    End Sub
End Module
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomerComplexAddress;

namespace CustomerComplexAddressClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (CustomerComplexAddressContext objCtx =
                    new CustomerComplexAddressContext())
                {
                    foreach (CCustomer customer in objCtx.CCustomers)
                    {
                        Console.WriteLine("Customer Id: " +
                            "{0} {1}\r\n{2} {3}," +
                            "{4} {5}\n\rPhone: {6}", 
                            customer.CustomerId.ToString(),
                            customer.CompanyName,
                            customer.Address.StreetAddress,
                            customer.Address.City,
                            customer.Address.Region,
                            customer.Address.PostalCode,
                            customer.Address.Phone);
                    }

                    
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

参照

処理手順

複合型を使用してモデルを定義する方法 (Entity Framework)
複合型を使用してオブジェクトを追加および変更する方法 (Entity Framework)

概念

複合型 (EDM)