Condividi tramite


EntityKey Classe

Definizione

Fornisce un riferimento durevole a un oggetto che è un'istanza di un tipo di entità.

public ref class EntityKey sealed : IEquatable<System::Data::EntityKey ^>
[System.Runtime.Serialization.DataContract(IsReference=true)]
[System.Serializable]
public sealed class EntityKey : IEquatable<System.Data.EntityKey>
[<System.Runtime.Serialization.DataContract(IsReference=true)>]
[<System.Serializable>]
type EntityKey = class
    interface IEquatable<EntityKey>
Public NotInheritable Class EntityKey
Implements IEquatable(Of EntityKey)
Ereditarietà
EntityKey
Attributi
Implementazioni

Esempio

Questi esempi illustrano come creare e usare un oggetto EntityKey.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    Object entity = null;
    IEnumerable<KeyValuePair<string, object>> entityKeyValues =
        new KeyValuePair<string, object>[] {
            new KeyValuePair<string, object>("SalesOrderID", 43680) };

    // Create the  key for a specific SalesOrderHeader object.
    EntityKey key = new EntityKey("AdventureWorksEntities.SalesOrderHeaders", entityKeyValues);

    // Get the object from the context or the persisted store by its key.
    if (context.TryGetObjectByKey(key, out entity))
    {
        Console.WriteLine("The requested " + entity.GetType().FullName +
            " object was found");
    }
    else
    {
        Console.WriteLine("An object with this key " +
            "could not be found.");
    }
}
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Create the key that represents the order.
        EntityKey orderKey =
            new EntityKey("AdventureWorksEntities.SalesOrderHeaders",
                "SalesOrderID", orderId);

        // Create the stand-in SalesOrderHeader object
        // based on the specified SalesOrderID.
        SalesOrderHeader order = new SalesOrderHeader();
        order.EntityKey = orderKey;

        // Assign the ID to the SalesOrderID property to matche the key.
        order.SalesOrderID = (int)orderKey.EntityKeyValues[0].Value;

        // Attach the stand-in SalesOrderHeader object.
        context.SalesOrderHeaders.Attach(order);

        // Create a new SalesOrderDetail object.
        // You can use the static CreateObjectName method (the Entity Framework
        // adds this method to the generated entity types) instead of the new operator:
        // SalesOrderDetail.CreateSalesOrderDetail(1, 0, 2, 750, 1, (decimal)2171.2942, 0, 0,
        //                                         Guid.NewGuid(), DateTime.Today));
        SalesOrderDetail detail = new SalesOrderDetail
        {
            SalesOrderID = orderId,
            SalesOrderDetailID = 0,
            OrderQty = 2,
            ProductID = 750,
            SpecialOfferID = 1,
            UnitPrice = (decimal)2171.2942,
            UnitPriceDiscount = 0,
            LineTotal = 0,
            rowguid = Guid.NewGuid(),
            ModifiedDate = DateTime.Now
        };

        order.SalesOrderDetails.Add(detail);

        context.SaveChanges();
    }
    catch (InvalidOperationException)
    {
        Console.WriteLine("Ensure that the key value matches the value of the object's ID property.");
    }
    catch (UpdateException)
    {
        Console.WriteLine("An error has occurred. Ensure that an object with the '{0}' key value exists.",
        orderId);
    }
}

Commenti

Gli EntityKey oggetti non sono modificabili, ovvero dopo la loro costruzione non possono essere modificati.

Costruttori

Nome Descrizione
EntityKey()

Inizializza una nuova istanza della classe EntityKey.

EntityKey(String, IEnumerable<EntityKeyMember>)

Inizializza una nuova istanza della EntityKey classe con un nome del set di entità e una IEnumerable<T> raccolta di EntityKeyMember oggetti.

EntityKey(String, IEnumerable<KeyValuePair<String,Object>>)

Inizializza una nuova istanza della EntityKey classe con un nome del set di entità e una raccolta generica KeyValuePair .

EntityKey(String, String, Object)

Inizializza una nuova istanza della EntityKey classe con un nome del set di entità e una coppia di chiavi di entità specifica.

Campi

Nome Descrizione
EntityNotValidKey

EntityKey Semplice identificazione di un'entità risultante da un'operazione TREAT non riuscita.

NoEntitySetKey

Singleton EntityKey in base al quale viene identificata un'entità di sola lettura.

Proprietà

Nome Descrizione
EntityContainerName

Ottiene o imposta il nome del contenitore di entità.

EntityKeyValues

Ottiene o imposta i valori di chiave associati all'oggetto EntityKey.

EntitySetName

Ottiene o imposta il nome del set di entità.

IsTemporary

Ottiene un valore che indica se l'oggetto EntityKey è temporaneo.

Metodi

Nome Descrizione
Equals(EntityKey)

Restituisce un valore che indica se questa istanza è uguale a un oggetto specificato EntityKey.

Equals(Object)

Restituisce un valore che indica se questa istanza è uguale a un oggetto specificato.

GetEntitySet(MetadataWorkspace)

Ottiene il set di entità per questa chiave di entità dall'area di lavoro metadati specificata.

GetHashCode()

Funge da funzione hash per l'oggetto corrente EntityKey . GetHashCode() è adatto per algoritmi di hashing e strutture di dati, ad esempio una tabella hash.

GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
OnDeserialized(StreamingContext)

Metodo helper utilizzato per deserializzare un oggetto EntityKey.

OnDeserializing(StreamingContext)

Metodo helper utilizzato per deserializzare un oggetto EntityKey.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Operatori

Nome Descrizione
Equality(EntityKey, EntityKey)

Confronta due oggetti EntityKey.

Inequality(EntityKey, EntityKey)

Confronta due oggetti EntityKey.

Si applica a