5

Given an entity framework 4.0 code first entity

public class MyEntity
{
  [Key]
  public int MyEntityId { get; set; }
}

Is there a way to retrieve the value of the property decorated with the [Key] attribute without knowing the property's name?

public class KeyReader<TEntity> : where TEntity : class
{
  public int GetKeyValue(TEntity entity){
    //get key value for entity
  }
}

UPDATE

I have a DBContext, so I can use the following code:

var objectContext = ((IObjectContextAdapter) myContext).ObjectContext;
objectContext.ObjectStateManager.GetObjectStateEntry(entity).EntityKey.EntityKeyValues;

However, this only works after the entity has been added or attached to the DBContext. The issue is that I wanted to use dynamic knowledge of the key and its value to determine whether an insert or update should be performed and thus whether to add or attach it to the context. By the time this code becomes possible, this is too late.

I have edited the question title to reflect this.

Any further thoughts?

Adam Flynn
  • 949
  • 2
  • 9
  • 21

2 Answers2

7

Another possible solution would be to use reflection to find the property which is decorated with the [Key] attribute and to return its value:

private object GetKeyValue<T>(T entity) where T : class
{
    PropertyInfo key =
        typeof(T)
        .GetProperties()
        .FirstOrDefault(p => p.GetCustomAttributes(typeof(KeyAttribute), true).Length != 0);

    return key != null ? key.GetValue(entity, null) : null;
}

MyEntity instanceOfMyEntity = new MyEntity { MyEntityId = 999; };
object keyValue = GetKeyValue<MyEntity>(instanceOfMyEntity); // keyValue == 999

Note that this method will only return the value of the first property which is decorated with the KeyAttribute, and will return null if a Key property is not found.

RoccoC5
  • 4,185
  • 16
  • 20
3

If you have the ObjectContext your entity is part of, you can call YourContext.GetObjectStateEntry(entity). This returns a corresponding ObjectStateEntry object, which contains an EntityKey property storing the key value(s).

If you're using DbContext, I do not see a similar function. However, even if it does not have one, you can still use IObjectContextAdapter (((IObjectContextAdapter)YourContext).ObjectContext) to get the underlying ObjectContext, and then use the above.

  • This is correct but see my update as to why this is unhelpful in solving the underlying intent. I up voted anyway, because I appreciate the suggestion. – Adam Flynn Feb 15 '12 at 18:38
  • You *could* attach it, use this to get the key values, and if you then see you should have added it, call `ObjectStateEntry.ChangeState(EntityState.Added)`. But really, that's doing the wrong thing first, checking whether you did it wrong, and then fixing it. The answer you accepted is probably a better idea. –  Feb 15 '12 at 19:09