Use the runtime type of the root (instead of the compile-time type which might be a base class) to create the "persisted" when adding a new root
Added a null-check for the entity and did some cleanup