I also faced with similar problem and found the following solution
// Entity Framework Core
public virtual int GetKey<T>(T entity)
{
var keyName = Context.Model.FindEntityType(typeof (T)).FindPrimaryKey().Properties
.Select(x => x.Name).Single();
return (int)entity.GetType().GetProperty(keyName).GetValue(entity, null);
}