共用方式為


HOW TO:使用複雜類型來建立和執行物件查詢 (Entity Framework)

此範例使用 HOW TO:定義具有複雜類型的模型 (Entity Framework) 主題中所定義的結構描述。

若要使用複雜類型建立專案

  1. 建立名為 CustomerComplexAddrClient 的主控台應用程式專案,並加入 System.Data.EntitySystem.Runtime.Serialization 的參考。

  2. 加入從 HOW TO:定義具有複雜類型的模型 (Entity Framework) 主題所述之專案建置之 dll 的參考。

  3. 將主題 HOW TO:定義具有複雜類型的模型 (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>

範例

此範例程式碼會顯示所有 CCustomersCAddress 複雜類型的內部屬性。此物件內容包含 CCustomers 的集合,且複雜屬性為 Address。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());
            }
        }
    }
}

另請參閱

工作

HOW TO:定義具有複雜類型的模型 (Entity Framework)
HOW TO:加入及修改具有複雜類型的物件 (Entity Framework)

概念

複雜類型 (EDM)