Compartir a través de


Cómo agregar y modificar objetos con tipos complejos (Entity Framework)

En este tema se usa el Entity Data Model diseñado en el tema Cómo definir un modelo con un tipo complejo (Entity Framework).

Para crear una aplicación y agregar el tipo complejo al almacenamiento

  1. Cree un proyecto de aplicación de consola denominado CustomerComplexAddrClient.

  2. Agregue referencias a los espacios de nombres System.Runtime.Serialization y System.Data.Entity.

  3. 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).

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

  5. Cree un archivo de configuración de la aplicación según se muestra a continuación.

  6. Copie el código de ejemplo al archivo Program.cs.

  7. Genere y ejecute el proyecto.

// The following syntax is used in the App.config file.
<?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=CustomerWComplexAddress;
         Integrated Security=True;
         multipleactiveresultsets=true&quot;"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

Ejemplo

El código de ejemplo mostrado crea una entidad de tipo CCustomer y una CAddress de tipo complejo. Las propiedades de los tipos CCustomer y CAddress se inicializan. El tipo CAddress se asigna a la propiedad Address del CCustomer. CCustomer y CAddress se agregan al almacenamiento y los cambios se guardan.

Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports CustomerComplexAddress


Module Module1

    Sub Main()
        Try
            Using objCtx As CustomerComplexAddressContext = _
                              New CustomerComplexAddressContext()

                Dim newCustomer12 As CCustomer = New CCustomer()
                newCustomer12.CustomerId = 12
                newCustomer12.ContactTitle = "Title 12"
                newCustomer12.ContactName = "Contact 12"
                newCustomer12.CompanyName = "Company 12"

                Dim newAddress12 As CAddress = New CAddress()
                newAddress12.StreetAddress = "1121 St"
                newAddress12.City = "Redmond"
                newAddress12.Region = "WA"
                newAddress12.Country = "USA"
                newAddress12.PostalCode = "97612"
                newAddress12.Phone = "2344567812"
                newAddress12.Fax = "3451238712"

                newCustomer12.Address = newAddress12

                objCtx.AddToCCustomers(newCustomer12)
                objCtx.SaveChanges()

            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 CustomerComplexAddrClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (CustomerComplexAddressContext objCtx =
                    new CustomerComplexAddressContext())
                {
                    CCustomer newCustomer10 = new CCustomer();
                    newCustomer10.CustomerId = 10;
                    newCustomer10.ContactTitle = "Title 10";
                    newCustomer10.ContactName = "Contact 10";
                    newCustomer10.CompanyName = "Company 10";

                    CAddress newAddress10 = new CAddress();
                    newAddress10.StreetAddress = "1001 St";
                    newAddress10.City = "Redmond";
                    newAddress10.Region = "WA";
                    newAddress10.Country = "USA";
                    newAddress10.PostalCode = "97600";
                    newAddress10.Phone = "2344567890";
                    newAddress10.Fax = "3451238700";

                    newCustomer10.Address = newAddress10;

                    objCtx.AddToCCustomers(newCustomer10);
                    objCtx.SaveChanges();
                    
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Vea también

Tareas

Cómo definir un modelo con un tipo complejo (Entity Framework)
Cómo crear y ejecutar consultas de objeto con tipos complejos (Entity Framework)

Conceptos

Tipo complejo (EDM)