Tweak to the logic when a context does not have an entity associated.
Improved exception handling.