Export (0) Print
Expand All

EntityKey Class

Provides a durable reference to an object that is an instance of an entity type.

Namespace:  System.Data
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

[SerializableAttribute]
[DataContractAttribute(IsReference = true)]
public sealed class EntityKey : IEquatable<EntityKey>

The EntityKey objects are immutable; that is, after they are constructed they cannot be modified.

For more information, see Working with Entity Keys (Entity Framework).

These examples are based on the Adventure Works Sales Model. The examples show you how to create and use an EntityKey.

using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        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.SalesOrderHeader", entityKeyValues);

        // Get the object from the context or the persisted store by its key. 
        if (advWorksContext.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.");
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

System.Object
  System.Data.EntityKey

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2014 Microsoft