Data Access has been discontinued. Please refer to this page for more information.

How to: Return a Specific Object Using its Key

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

This topic shows how to retrieve a data type by its key values instead of explicitly creating and executing a query. This example uses the TryGetObject<T> method on OpenAccessContext to return an object with the specified key into the object context.

To run the code in this example, you need to create a new Telerik Data Access Domain Model based on the Northwind database.

The following code is the LINQ example.

using (EntitiesModel dbContext = new EntitiesModel())
{
   Telerik.OpenAccess.ObjectKey objectKey = new Telerik.OpenAccess.ObjectKey("Customer", 
       new[] { new Telerik.OpenAccess.ObjectKeyMember("CustomerID", "ALFKI") });
   Customer customer = null;
   if (dbContext.TryGetObjectByKey<Customer>(objectKey, out customer))
   {
       Console.WriteLine(customer.CompanyName);
   }
}
Using dbContext As New EntitiesModel()
 Dim objectKey As New Telerik.OpenAccess.ObjectKey("Customer", 
    { New Telerik.OpenAccess.ObjectKeyMember("CustomerID", "ALFKI") })
 Dim customer_Renamed As Customer = Nothing
 If dbContext.TryGetObjectByKey(Of Customer)(objectKey, customer_Renamed) Then
  Console.WriteLine(customer_Renamed.CompanyName)
 End If
End Using

For more information about the ObjectKey API, please refer to the following topics:

See Also