Given the essence of Framework 4.0, the code of the first entity
public class MyEntity
{
[Key]
public int MyEntityId { get; set; }
}
Is there a way to get the value of a property decorated with the [Key] attribute without knowing the name of the property?
public class KeyReader<TEntity> : where TEntity : class
{
public int GetKeyValue(TEntity entity){
}
}
UPDATE
I have a DBC text, so I can use the following code:
var objectContext = ((IObjectContextAdapter) myContext).ObjectContext;
objectContext.ObjectStateManager.GetObjectStateEntry(entity).EntityKey.EntityKeyValues;
However, this only works after adding or attaching the object to the DBContext. The problem is that I wanted to use dynamic knowledge of the key and its value to determine whether to insert or update, and therefore add or attach it to the context. By the time this code appears, it's too late.
I edited the title of the question to reflect this.
Any further thoughts?