Procedura: creare ed eseguire query di oggetto con tipi complessi (Entity Framework)

In questo esempio vengono utilizzati gli schemi definiti nell'argomento Procedura: definire un modello con un tipo complesso (Entity Framework).

Per creare il progetto utilizzando tipi complessi

  1. Creare un progetto di applicazione console denominato CustomerComplexAddrClient e aggiungere riferimenti a System.Data.Entity e System.Runtime.Serialization.

  2. Aggiungere un riferimento alla DLL compilata in base al progetto descritto nell'argomento Procedura: definire un modello con un tipo complesso (Entity Framework).

  3. Aggiungere gli schemi inclusi nell'argomento Procedura: definire un modello con un tipo complesso (Entity Framework) alla stessa cartella del file eseguibile.

  4. Creare un file di configurazione dell'applicazione come illustrato nell'esempio.

  5. Copiare il codice dell'esempio nel file Program.cs.

  6. Aggiungere un file di configurazione dell'applicazione con il contenuto seguente.

  7. Compilare ed eseguire il progetto.

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

Esempio

Il codice di esempio consente di visualizzare tutti gli oggetti CCustomers e le proprietà interne del tipo complesso CAddress. Il contesto dell'oggetto contiene un insieme di CCustomers con la proprietà complessa Address. La proprietà Address è di tipo CAddress. Tutte le relative proprietà interne sono accessibili dalle istanze del tipo 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());
            }
        }
    }
}

Vedere anche

Attività

Procedura: definire un modello con un tipo complesso (Entity Framework)
Procedura: aggiungere e modificare oggetti con tipi complessi (Entity Framework)

Concetti

Tipo complesso (EDM)