Gewusst wie: Verwenden von EntityReference zum Ändern von Beziehungen zwischen Objekten (Entity Framework)
In diesem Thema wird dargestellt, wie das EntityReference-Objekt verwendet wird, um eine Beziehung zwischen zwei Objekten im Objektkontext zu ändern. Wenn die SaveChanges-Methode aufgerufen wird, wird die Änderung in der Datenbank dauerhaft als eine Änderung am Fremdschlüssel der verknüpften Tabelle gespeichert. Weitere Informationen finden Sie unter Gewusst wie: Ausführen von Geschäftslogik im Verlauf von Zuordnungsänderungen.
Das Beispiel in diesem Thema beruht auf dem Adventure Works Sales-Modell. Zum Ausführen des Codes in diesem Thema muss dem Projekt bereits das Adventure Works Sales-Modell hinzugefügt und das Projekt zur Verwendung von Entity Framework konfiguriert worden sein. Weitere Informationen finden Sie unter Gewusst wie: Verwenden des Assistenten für Entity Data Model (Entity Framework) bzw. Gewusst wie: Manuelles Konfigurieren eines Entity Framework-Projekts und Gewusst wie: Manuelles Definieren eines Entity Data Model (Entity Framework).
Beispiel
In diesem Beispiel wird aufgezeigt, wie das EntityReference-Objekt verwendet wird, um die Beziehung zwischen einem SalesOrderHeader-Objekt und einem verbundenen Address-Objekt zu ändern, das die Lieferadresse der Bestellung darstellt.
' Define the order and new address IDs.
Dim orderId As Integer = 43669
Dim addressId As Integer = 26
Using context As New AdventureWorksEntities()
' Get the billing address to change to.
Dim address As Address = context.Addresses.Single(Function(c) c.AddressID = addressId)
' Get the order being changed.
Dim order As SalesOrderHeader = context.SalesOrderHeaders.Single(Function(o) o.SalesOrderID = orderId)
' You do not have to call the Load method to load the addresses for the order,
' because lazy loading is set to true
' by the constructor of the AdventureWorksEntities object.
' With lazy loading set to true the related objects are loaded when
' you access the navigation property. In this case Address.
' Write the current billing street address.
Console.WriteLine("Current street: " & order.Address.AddressLine1)
' Change the billing address.
If Not order.Address.Equals(address) Then
' Use Address navigation property to change the association.
order.Address = address
' Write the changed billing street address.
Console.WriteLine("Changed street: " & order.Address.AddressLine1)
End If
' If the address change succeeds, save the changes.
context.SaveChanges()
' Write the current billing street address.
Console.WriteLine("Current street: " & order.Address.AddressLine1)
End Using
// Define the order and new address IDs.
int orderId = 43669;
int addressId = 26;
using (AdventureWorksEntities context
= new AdventureWorksEntities())
{
// Get the billing address to change to.
Address address =
context.Addresses.Single(c => c.AddressID == addressId);
// Get the order being changed.
SalesOrderHeader order =
context.SalesOrderHeaders.Single(o => o.SalesOrderID == orderId);
// You do not have to call the Load method to load the addresses for the order,
// because lazy loading is set to true
// by the constructor of the AdventureWorksEntities object.
// With lazy loading set to true the related objects are loaded when
// you access the navigation property. In this case Address.
// Write the current billing street address.
Console.WriteLine("Current street: "
+ order.Address.AddressLine1);
// Change the billing address.
if (!order.Address.Equals(address))
{
// Use Address navigation property to change the association.
order.Address = address;
// Write the changed billing street address.
Console.WriteLine("Changed street: "
+ order.Address.AddressLine1);
}
// If the address change succeeds, save the changes.
context.SaveChanges();
// Write the current billing street address.
Console.WriteLine("Current street: "
+ order.Address.AddressLine1);
}
Siehe auch
Aufgaben
Gewusst wie: Verwenden der Fremdschlüsseleigenschaft, um Beziehungen zwischen Objekten zu ändern
Gewusst wie: Ausführen von Geschäftslogik im Verlauf von Zuordnungsänderungen
Konzepte
Arbeiten mit Objekten (Entity Framework)
Definieren und Verwalten von Beziehungen (Entity Framework)