Freigeben über


Gewusst wie: Hinzufügen und Ändern von Objekten mit komplexen Typen (Entity Framework)

In diesem Thema wird das im Thema Gewusst wie: Definieren eines Modells mit komplexen Typen (Entity Framework) entworfene Entity Data Model verwendet.

So erstellen Sie eine Anwendung und fügen dem Speicher einen komplexen Typ hinzu

  1. Erstellen Sie ein Konsolenanwendungsprojekt mit dem Namen "CustomerComplexAddrClient".

  2. Fügen Sie Verweise auf den System.Data.Entity-Namespace und den System.Runtime.Serialization-Namespace hinzu.

  3. Fügen Sie einen Verweis auf die DLL hinzu, die von dem im Thema Gewusst wie: Definieren eines Modells mit komplexen Typen (Entity Framework) beschriebenen Projekt erstellt wird.

  4. Fügen Sie die Schemas aus dem Thema Gewusst wie: Definieren eines Modells mit komplexen Typen (Entity Framework) dem Ordner hinzu, in dem sich die ausführbare Datei befindet.

  5. Erstellen Sie eine Anwendungskonfigurationsdatei, wie im folgenden Beispiel.

  6. Kopieren Sie den Beispielcode in die Datei Program.cs.

  7. Erstellen Sie das Projekt, und führen Sie es aus.

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

Beispiel

Im gezeigten Beispielcode werden eine Entität des CCustomer und ein CAddress des komplexen Typs erstellt. Die Eigenschaften von CCustomer und CAddress werden initialisiert. Die CAddress wird der Address-Eigenschaft von CCustomer zugewiesen. Sowohl CCustomer als auch CAddress werden dem Speicher hinzugefügt, und die Änderungen werden gespeichert.

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

Siehe auch

Aufgaben

Gewusst wie: Definieren eines Modells mit komplexen Typen (Entity Framework)
Gewusst wie: Erstellen und Ausführen von Objektabfragen mit komplexen Typen (Entity Framework)

Konzepte

Komplexer Typ (EDM)