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 argomento viene fornito un esempio di come applicare a un oggetto gli aggiornamenti apportati a un'istanza disconnessa dello stesso oggetto. Questa procedura viene utilizzata quando un oggetto viene aggiornato in modalità remota e viene impostato di nuovo nel server per rendere persistenti le modifiche. Se l'oggetto venisse semplicemente connesso a un contesto dell'oggetto nel server, gli aggiornamenti andrebbero persi o l'operazione avrebbe esito negativo nel caso in cui l'oggetto sia già nel contesto dell'oggetto. Questo si verifica in quanto gli oggetti vengono connessi in un stato Unchanged. Per ulteriori informazioni, vedere Connessione di oggetti (Entity Framework).
L'esempio incluso in questo argomento è basato sul modello Sales di AdventureWorks. Per eseguire il codice incluso in questo esempio, è necessario avere già aggiunto il modello Sales di AdventureWorks al progetto e avere configurato il progetto per l'utilizzo di Entity Framework. A tale scopo, completare le procedure descritte in Procedura: configurare manualmente un progetto di Entity Framework e Procedura: definire manualmente un modello EDM (Entity Framework).
Esempio
Nell'esempio seguente un oggetto SalesOrderDetail viene passato al metodo UpdateItemChanges insieme all'oggetto originale. In questo modo le modifiche vengono applicate senza eseguire query per l'oggetto o doverlo rendere persistente in memoria.
Private Shared Sub ApplyItemUpdates(ByVal originalItem As SalesOrderDetail, _
ByVal updatedItem As SalesOrderDetail)
Using advWorksContext As AdventureWorksEntities = _
New AdventureWorksEntities()
Try
' Attach the original item to the object context.
advWorksContext.Attach(originalItem)
' Call the ApplyPropertyChanges method to apply changes
' from the updated item to the original version.
advWorksContext.ApplyPropertyChanges( _
"SalesOrderDetail", updatedItem)
advWorksContext.SaveChanges()
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Using
End Sub
private static void ApplyItemUpdates(SalesOrderDetail originalItem,
SalesOrderDetail updatedItem)
{
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
try
{
// Attach the original item to the object context.
advWorksContext.Attach(originalItem);
// Call the ApplyPropertyChanges method to apply changes
// from the updated item to the original version.
advWorksContext.ApplyPropertyChanges("SalesOrderDetail",
updatedItem);
advWorksContext.SaveChanges();
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
In questo esempio l'oggetto SalesOrderDetail originale viene recuperato, quindi vengono applicate le modifiche in base a un oggetto SalesOrderDetail aggiornato passato al metodo UpdateItemChanges.
Private Shared Sub ApplyItemUpdates(ByVal updatedItem As SalesOrderDetail)
' Define an ObjectStateEntry and EntityKey for the current object.
Dim key As EntityKey
Dim originalItem As Object
Using advWorksContext As AdventureWorksEntities = _
New AdventureWorksEntities()
Try
' Create the detached object's entity key.
key = advWorksContext.CreateEntityKey("SalesOrderDetail", updatedItem)
' Get the original item based on the entity key from the context
' or from the database.
If advWorksContext.TryGetObjectByKey(key, originalItem) Then
' Call the ApplyPropertyChanges method to apply changes
' from the updated item to the original version.
advWorksContext.ApplyPropertyChanges( _
key.EntitySetName, _
updatedItem)
End If
advWorksContext.SaveChanges()
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Using
End Sub
private static void ApplyItemUpdates(SalesOrderDetail updatedItem)
{
// Define an ObjectStateEntry and EntityKey for the current object.
EntityKey key;
object originalItem;
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
try
{
// Create the detached object's entity key.
key = advWorksContext.CreateEntityKey("SalesOrderDetail", updatedItem);
// Get the original item based on the entity key from the context
// or from the database.
if (advWorksContext.TryGetObjectByKey(key, out originalItem))
{
// Call the ApplyPropertyChanges method to apply changes
// from the updated item to the original version.
advWorksContext.ApplyPropertyChanges(
key.EntitySetName, updatedItem);
}
advWorksContext.SaveChanges();
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
Vedere anche
Concetti
Servizi Web e Entity Data Model (scenari applicativi)
Serializzazione di oggetti (Entity Framework)
Connessione di oggetti (Entity Framework)