EntityKey Klasa

Definicja

Zapewnia trwałe odwołanie do obiektu, który jest wystąpieniem typu jednostki.

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)
Dziedziczenie
EntityKey
Atrybuty
Implementuje

Przykłady

W tych przykładach pokazano, jak utworzyć i użyć elementu 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);
    }
}

Uwagi

Obiekty EntityKey są niezmienne. Oznacza to, że po ich konstruowaniu nie można ich modyfikować.

Aby uzyskać więcej informacji, zobacz Praca z kluczami jednostek.

Konstruktory

EntityKey()

Inicjuje nowe wystąpienie klasy EntityKey.

EntityKey(String, IEnumerable<EntityKeyMember>)

Inicjuje EntityKey nowe wystąpienie klasy z nazwą zestawu jednostek i kolekcją IEnumerable<T>EntityKeyMember obiektów.

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

Inicjuje EntityKey nowe wystąpienie klasy z nazwą zestawu jednostek i kolekcją ogólną KeyValuePair .

EntityKey(String, String, Object)

Inicjuje EntityKey nowe wystąpienie klasy z nazwą zestawu jednostek i określoną parą kluczy jednostki.

Pola

EntityNotValidKey

Prosta EntityKey identyfikacja jednostki, która wynika z nieudanej operacji TREAT .

NoEntitySetKey

Pojedyncza jednostka EntityKey , za pomocą której jest identyfikowana jednostka tylko do odczytu.

Właściwości

EntityContainerName

Pobiera lub ustawia nazwę kontenera jednostki.

EntityKeyValues

Pobiera lub ustawia wartości klucza skojarzone z tym EntityKeyelementem .

EntitySetName

Pobiera lub ustawia nazwę zestawu jednostek.

IsTemporary

Pobiera wartość wskazującą, czy EntityKey parametr jest tymczasowy.

Metody

Equals(EntityKey)

Zwraca wartość wskazującą, czy to wystąpienie jest równe określonej EntityKeywartości .

Equals(Object)

Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi.

GetEntitySet(MetadataWorkspace)

Pobiera zestaw jednostek dla tego klucza jednostki z danego obszaru roboczego metadanych.

GetHashCode()

Służy jako funkcja skrótu dla bieżącego EntityKey obiektu. GetHashCode() nadaje się do tworzenia skrótów algorytmów i struktur danych, takich jak tabela skrótów.

GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
OnDeserialized(StreamingContext)

Metoda pomocnika używana do deserializacji elementu EntityKey.

OnDeserializing(StreamingContext)

Metoda pomocnika używana do deserializacji elementu EntityKey.

ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Operatory

Equality(EntityKey, EntityKey)

Porównuje dwa EntityKey obiekty.

Inequality(EntityKey, EntityKey)

Porównuje dwa EntityKey obiekty.

Dotyczy