Compartir a través de


Cómo crear y ejecutar consultas de objeto con tipos complejos (Entity Framework)

En este ejemplo se usan los esquemas definidos en el tema Cómo definir un modelo con un tipo complejo (Entity Framework).

Para crear el proyecto con tipos complejos

  1. Cree un proyecto de aplicación de consola denominado CustomerComplexAddrClient y agregue referencias a System.Data.Entity y System.Runtime.Serialization.

  2. Agregue una referencia a la biblioteca DLL generada con el proyecto que se describe en el tema Cómo definir un modelo con un tipo complejo (Entity Framework).

  3. Agregue los esquemas del tema Cómo definir un modelo con un tipo complejo (Entity Framework) a la misma carpeta donde está el ejecutable.

  4. Cree un archivo de configuración de la aplicación según se muestra en el ejemplo.

  5. Copie el código del ejemplo al archivo Program.cs.

  6. Agregue un archivo de configuración de la aplicación con el contenido que se muestra a continuación.

  7. Genere y ejecute el proyecto.

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

Ejemplo

En el código de ejemplo se muestran todos los CCustomers y las propiedades internas del tipo complejo CAddress. El objeto contiene una colección de CCustomers con la propiedad compleja Address. La propiedad Address es de tipo CAddress. Todas sus propiedades internas son accesibles desde instancias 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());
            }
        }
    }
}

Vea también

Tareas

Cómo definir un modelo con un tipo complejo (Entity Framework)
Cómo agregar y modificar objetos con tipos complejos (Entity Framework)

Conceptos

Tipo complejo (EDM)