ObjectContext.GetObjectByKey(EntityKey) Yöntem

Tanım

Belirtilen varlık anahtarına sahip bir nesne döndürür.

public:
 System::Object ^ GetObjectByKey(System::Data::EntityKey ^ key);
public object GetObjectByKey(System.Data.EntityKey key);
member this.GetObjectByKey : System.Data.EntityKey -> obj
Public Function GetObjectByKey (key As EntityKey) As Object

Parametreler

key
EntityKey

Bulunabilecek nesnenin anahtarı.

Döndürülenler

Varlık Object türünün örneği olan.

Özel durumlar

key parametresi null'dir.

Nesne veya veri kaynağında ObjectStateManager bulunamadı.

Örnekler

Bu örnek, verilen türde bir varlık için bir EntityKey oluşturur ve ardından anahtara göre bir varlık getirir.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Define the entity key values.
        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.
        SalesOrderHeader order =
            (SalesOrderHeader)context.GetObjectByKey(key);

        Console.WriteLine("SalesOrderID: {0} Order Number: {1}",
            order.SalesOrderID, order.SalesOrderNumber);
    }
    catch (ObjectNotFoundException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Açıklamalar

GetObjectByKeyöğesinden ObjectStateManagerbelirtilen EntityKey bir nesneyi almaya çalışır. Nesne şu anda nesne bağlamı içine yüklenmemişse, veri kaynağından nesneyi döndürme girişiminde bir sorgu yürütülür.

GetObjectByKey nesne bulunamadığında bir ObjectNotFoundException oluşturur. Bu özel durumun işlenmesini önlemek için yöntemini kullanın TryGetObjectByKey .

Bu yöntem durumundaki Deleted nesneleri döndürür.

Geçici anahtar, veri kaynağından bir nesne döndürmek için kullanılamaz.

Şunlara uygulanır

Ayrıca bkz.