15

Shouldn't I be able to get the EntityKey object using the complex property method or property method for the DbEntityEntry. I couldn't find any examples MSDN, but I presume that this is possible in Entity Framework 5. I Will not know the name of the entity key or entity as I am using a generic repository interface.

3
  • Can you share your code? Can you share the exact error/exception that you are getting? Please don't include tags in your title. Is this specific to Entity Framework 5?
    – Ryan Gates
    Apr 9, 2013 at 4:17
  • Yes it is specific to Entity Framework 5 , thats why I specified it in the title and my actual question.
    – Breadtruck
    Apr 9, 2013 at 4:31
  • No code because it is just a theoretical question. I have found other ways to get the primary key value of an entity, but I was curious if you could get at it from this path
    – Breadtruck
    Apr 9, 2013 at 4:37

1 Answer 1

36

If you have a DbEntityEntry object you get the EntityKey by first finding the wrapped ObjectContext:

var oc = ((IObjectContextAdapter)dbContext).ObjectContext;

Then you can find the entity key by

oc.ObjectStateManager.GetObjectStateEntry(dbEntityEntryObject.Entity)
    .EntityKey

EDIT

I created two extension methods that get you close to what you want:

public static EntityKey GetEntityKey<T>(this DbContext context, T entity)
    where T : class
{
    var oc = ((IObjectContextAdapter)context).ObjectContext;
    ObjectStateEntry ose;
    if (null != entity && oc.ObjectStateManager
                            .TryGetObjectStateEntry(entity, out ose))
    {
        return ose.EntityKey;
    }
    return null;
}

public static EntityKey GetEntityKey<T>( this DbContext context
                                       , DbEntityEntry<T> dbEntityEntry)
    where T : class
{
    if (dbEntityEntry != null)
    {
        return GetEntityKey(context, dbEntityEntry.Entity);
    }
    return null;
}

Now you can do

var entityKey = dbContext.GetEntityKey(entity);

or

var entityKey = dbContext.GetEntityKey(dbEntityEntryObject);

The runtime will pick the right overload.

Note that the syntax that you proposed (dbEntityEntryObject.Property<EntityKey>()) can't work when the entity has a composite key. You have to get the EntityKey from the entity itself.

6
  • I have seen this snippet of code in some other stackoverflow answers I just thought that maybe with EF 5.0 you could maybe do something like dbEntityEntryObject.Property<EntityKey>(); It seems to me that it would have been nice to expose the EntityKey as a property
    – Breadtruck
    Apr 9, 2013 at 14:51
  • 1
    I even think it should not be hard to introduce a property dbEntityEntryObject.EntityKey. You could submit a change request, or check if it's already there: entityframework.codeplex.com/workitem/list/basic. Apr 9, 2013 at 15:08
  • Thanks, those extension methods will come in handy. I suppose in the end your solution is probably how the ef code base would be changed anyway.
    – Breadtruck
    Apr 10, 2013 at 8:53
  • Doesn't work at all for me, EF6, model first, T4 templates, this always returns null for objects that are newly created ... solutions? May 14, 2014 at 13:12
  • @Mvision Only attached entities have entity keys. May 14, 2014 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.