Simplify entity primary key setting

Dont require setting primary key to nil when using custom primary key.
Refactor entity function generation