I am trying to create a generic method using EF4 to find the primary key of an object.
example
public string GetPrimaryKey()
{
...
}
>
this seems needlessly long? I have had the same need, and using the suggestions above (by SethO and denis_n), i am using:
//get the primary key field name and location for the table
var primaryFieldName = entry.EntitySet.ElementType.KeyMembers[0].Name ;
int primaryFieldLocation = entry.CurrentValues.GetOrdinal(primaryFieldName);
//gets the value pair for the primary key (returns field name + value)
var primaryField = entry.EntityKey.EntityKeyValues[primaryFieldLocation];
String primaryFieldValue = primaryField.Value.ToString();
Hope this helps anyone who is interested