EntityKey Třída

Definice

Poskytuje trvalý odkaz na objekt, který je instancí typu entity.

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)
Dědičnost
EntityKey
Atributy
Implementuje

Příklady

Tyto příklady ukazují, jak vytvořit a použít 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);
    }
}

Poznámky

Objekty EntityKey jsou neměnné, to znamená, že po jejich vytvoření je nelze upravit.

Další informace najdete v tématu Práce s klíči entit.

Konstruktory

EntityKey()

Inicializuje novou instanci EntityKey třídy.

EntityKey(String, IEnumerable<EntityKeyMember>)

Inicializuje novou instanci EntityKey třídy s názvem sady entit a IEnumerable<T> kolekcí EntityKeyMember objektů.

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

Inicializuje novou instanci EntityKey třídy s názvem sady entit a obecnou KeyValuePair kolekcí.

EntityKey(String, String, Object)

Inicializuje novou instanci EntityKey třídy s názvem sady entit a konkrétním párem klíčů entity.

Pole

EntityNotValidKey

Jednoduchá EntityKey identifikace entity, která byla výsledkem neúspěšné operace TREAT .

NoEntitySetKey

Jednoúčel EntityKey , podle kterého je identifikována entita jen pro čtení.

Vlastnosti

EntityContainerName

Získá nebo nastaví název kontejneru entit.

EntityKeyValues

Získá nebo nastaví hodnoty klíče přidružené k tomuto EntityKey.

EntitySetName

Získá nebo nastaví název sady entit.

IsTemporary

Získá hodnotu, která označuje, zda EntityKey je dočasný.

Metody

Equals(EntityKey)

Vrátí hodnotu, která označuje, zda se tato instance rovná zadané EntityKeyinstanci .

Equals(Object)

Vrací hodnotu, která určuje, zda je tato instance rovna zadanému objektu.

GetEntitySet(MetadataWorkspace)

Získá sadu entity pro tento klíč entity z daného pracovního prostoru metadat.

GetHashCode()

Slouží jako hashovací funkce pro aktuální EntityKey objekt. GetHashCode() je vhodný pro hashování algoritmů a datových struktur, jako je například hashovací tabulka.

GetType()

Získá aktuální Type instanci.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
OnDeserialized(StreamingContext)

Pomocná metoda, která se používá k deserializaci objektu EntityKey.

OnDeserializing(StreamingContext)

Pomocná metoda, která se používá k deserializaci objektu EntityKey.

ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Operátory

Equality(EntityKey, EntityKey)

Porovná dva EntityKey objekty.

Inequality(EntityKey, EntityKey)

Porovná dva EntityKey objekty.

Platí pro