ObjectContext.CreateEntityKey(String, Object) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Crea la chiave di entità per un oggetto specifico o restituisce la chiave di entità se esiste già.
public:
System::Data::EntityKey ^ CreateEntityKey(System::String ^ entitySetName, System::Object ^ entity);
public System.Data.EntityKey CreateEntityKey(string entitySetName, object entity);
member this.CreateEntityKey : string * obj -> System.Data.EntityKey
Public Function CreateEntityKey (entitySetName As String, entity As Object) As EntityKey
Parametri
- entitySetName
- String
Nome completo del set di entità a cui appartiene l'oggetto entità.
- entity
- Object
Oggetto per il quale viene recuperata la chiave di entità.
Valori restituiti
Oggetto EntityKey dell'oggetto .
Eccezioni
Quando uno dei due parametri è null.
Quando entitySetName è vuoto.
oppure
Quando il tipo dell'oggetto entity non esiste nel set di entità.
oppure
Quando l'oggetto entitySetName non è completo.
Quando la chiave di entità non può essere costruita correttamente in base ai parametri forniti.
Esempio
In questo esempio viene CreateEntityKey usato per recuperare la chiave di entità di un oggetto esistente.
private static void ApplyItemUpdates(SalesOrderDetail updatedItem)
{
// Define an ObjectStateEntry and EntityKey for the current object.
EntityKey key = default(EntityKey);
object originalItem = null;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Create the detached object's entity key.
key = context.CreateEntityKey("SalesOrderDetails", updatedItem);
// Get the original item based on the entity key from the context
// or from the database.
if (context.TryGetObjectByKey(key, out originalItem))
{
// Call the ApplyCurrentValues method to apply changes
// from the updated item to the original version.
context.ApplyCurrentValues(key.EntitySetName, updatedItem);
}
context.SaveChanges();
}
}
Commenti
Se un oggetto EntityKey non esiste per , entityil CreateEntityKey metodo crea una nuova chiave.
Questo metodo viene utilizzato per determinare se un oggetto con lo stesso EntityKey oggetto è già associato all'oggetto ObjectContext. Se un oggetto con lo stesso EntityKey oggetto è già associato, viene generata un'eccezione. Utilizzare il CreateEntityKey metodo per provare a recuperare l'oggetto EntityKey scollegato prima di chiamare il Attach metodo .