通过


EntityKey 类

定义

提供对作为实体类型的实例的对象的持久引用。

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)
继承
EntityKey
属性
实现

示例

这些示例演示如何创建和使用 .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);
    }
}

注解

这些 EntityKey 对象是不可变的;也就是说,在构造对象后,无法修改它们。

构造函数

名称 说明
EntityKey()

初始化 EntityKey 类的新实例。

EntityKey(String, IEnumerable<EntityKeyMember>)

使用实体集名称和IEnumerable<T>对象的集合EntityKeyMember初始化类的新实例EntityKey

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

使用实体集名称和泛型KeyValuePair集合初始化类的新实例EntityKey

EntityKey(String, String, Object)

使用实体集名称和特定实体密钥对初始化类的新实例 EntityKey

字段

名称 说明
EntityNotValidKey

EntityKey一个简单的标识由失败的 TREAT 操作导致的实体。

NoEntitySetKey

标识只读实体的单一实例 EntityKey

属性

名称 说明
EntityContainerName

获取或设置实体容器的名称。

EntityKeyValues

获取或设置与此 EntityKey关联的键值。

EntitySetName

获取或设置实体集的名称。

IsTemporary

获取一个值,该值指示是否为临时值 EntityKey

方法

名称 说明
Equals(EntityKey)

返回一个值,该值指示此实例是否等于指定的 EntityKey实例。

Equals(Object)

返回一个值,该值指示此实例是否等于指定对象。

GetEntitySet(MetadataWorkspace)

从给定的元数据工作区中获取此实体键的实体集。

GetHashCode()

用作当前 EntityKey 对象的哈希函数。 GetHashCode() 适用于哈希算法和数据结构,例如哈希表。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
OnDeserialized(StreamingContext)

用于反序列化的 EntityKey帮助程序方法。

OnDeserializing(StreamingContext)

用于反序列化的 EntityKey帮助程序方法。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

运营商

名称 说明
Equality(EntityKey, EntityKey)

比较两个 EntityKey 对象。

Inequality(EntityKey, EntityKey)

比较两个 EntityKey 对象。

适用于