How to create a SubScene via code

I’m writing an editor tool that imports a custom file, which now must create a SubScene in order for ECS to work. I can’t figure out how to convert a new scene added through
EditorSceneManager.NewScene to a sub scene, so I’m doing it manually:

_tableScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
_tableScene.name = tableName;
SceneManager.SetActiveScene(_tableScene);

// do the import, e.g. add game objects to the new scene

var scenePath = GetScenePath(tableName);
EditorSceneManager.SaveScene(_tableScene, scenePath);
EditorSceneManager.CloseScene(_tableScene, true);

var subSceneGo = new GameObject(tableName);
var subSceneMb = subSceneGo.AddComponent<SubScene>();
var subSceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath);
subSceneMb.SceneAsset = subSceneAsset;

This results in the SubScene being correctly saved and referenced, but there are two problems:

  • The scene is not loaded, i.e. invisible, and I have to click the checkbox to see it
  • There are ECS errors popping up, like this one:
NullReferenceException: Object reference not set to an instance of an object
Unity.Scenes.Editor.SubSceneInspectorUtility.GetLoadableScenes (Unity.Scenes.SubScene[] scenes) (at Library/PackageCache/com.unity.entities@1.0.0-pre.15/Unity.Scenes.Editor/SubSceneInspectorUtility.cs:110)
Unity.Scenes.Editor.SubSceneInspector.OnInspectorGUI () (at Library/PackageCache/com.unity.entities@1.0.0-pre.15/Unity.Scenes.Editor/SubSceneInspector.cs:343)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass71_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <f7044ab663d344a2badf1160e57d1c1d>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

Any help would be appreciated, there must be a simpler way?

maybe you can use these runtime apis from your editor tool ?
https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/content-management.html