Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
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
Creare un progetto di applicazione console denominato CustomerComplexAddrClient e aggiungere riferimenti a System.Data.Entity e System.Runtime.Serialization.
Aggiungere un riferimento alla DLL compilata in base al progetto descritto nell'argomento Procedura: definire un modello con un tipo complesso (Entity Framework).
Aggiungere gli schemi inclusi nell'argomento Procedura: definire un modello con un tipo complesso (Entity Framework) alla stessa cartella del file eseguibile.
Creare un file di configurazione dell'applicazione come illustrato nell'esempio.
Copiare il codice dell'esempio nel file Program.cs.
Aggiungere un file di configurazione dell'applicazione con il contenuto seguente.
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="
Data Source=serverName;
Initial Catalog=CustomerWComplexAddr;
Integrated Security=True;
multipleactiveresultsets=true""
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)