How to get the Primary Key(s) in Entity Framework 4.1, i.e. using DbContext

后端 未结 5 1915
逝去的感伤
逝去的感伤 2020-12-29 08:52

The answers I\'m seeing here are for ObjectContext. Is there a property to determine an entity\'s primary key names when using DbContext?

Ah.. one of those times tha

5条回答
  •  佛祖请我去吃肉
    2020-12-29 09:54

    You could find primary key value from EntityKey class (http://msdn.microsoft.com/en-us/library/system.data.entitykey.aspx).

    and you could find EntityKey object from DbContext like so :

    ObjectContext context = ((IObjectContextAdapter)dbContext).ObjectContext;
    EntityKey key = context.ObjectStateManager.GetObjectStateEntry(model).EntityKey;
    

提交回复
热议问题