Skip to content

Instantly share code, notes, and snippets.

@cmatskas
Last active August 29, 2015 14:21
Show Gist options
  • Save cmatskas/7751830733a1d2b32c09 to your computer and use it in GitHub Desktop.
Save cmatskas/7751830733a1d2b32c09 to your computer and use it in GitHub Desktop.
private int GetPrimaryKey(DbEntityEntry entry)
{
var myObject = entry.Entity;
var property =
myObject.GetType()
.GetProperties()
.FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(KeyAttribute)));
return (int)property.GetValue(myObject, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment