共用方式為


How to: Create and Execute Object Queries with Complex Types (Entity Framework)

This example uses the schemas defined in the topic How to: Define a Model with Complex Type (Entity Framework).

To create the project using complex types

  1. Create a console application project named CustomerComplexAddrClient and add references to System.Data.Entity and System.Runtime.Serialization.

  2. Add a reference to the dll built from the project described in the topic How to: Define a Model with Complex Type (Entity Framework).

  3. Add the schemas from the topic How to: Define a Model with Complex Type (Entity Framework) to the same folder as the executable.

  4. Create an Application Configuration file as shown in the example.

  5. Copy the code in the example to the Program.cs file.

  6. Add an application configuration file with the content shown below.

  7. Build and run the project.

<?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>

Example

The example code displays all CCustomers and the inner properties of the CAddress complex type. The object context contains a collection of CCustomers with the complex property Address. The Address property is of type CAddress. All its internal properties are accessible from instances of the CCustomer type.

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());
            }
        }
    }
}

See Also

Tasks

How to: Define a Model with Complex Type (Entity Framework)
How to: Add and Modify Objects with Complex Types (Entity Framework)

Concepts

Complex Type (EDM)