Is there such a method?
object GetPrimaryKeyValue(DbEntityEntry entry);
Or how should it be implemented?
You need to cast your DbContext
to IObjectContextAdapter
so you can access the underlying ObjectContext
which gives you access to some more advanced features hidden by DbContext
.
Inside your class which derives DbContext
the following method will work.
object GetPrimaryKeyValue(DbEntityEntry entry)
{
var objectStateEntry = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager.GetObjectStateEntry(entry.Entity);
return objectStateEntry.EntityKey.EntityKeyValues[0].Value;
}
If there is more than one key then you should iterate over the EntityKeyValues
property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With