When and how to add debug values in ECS

I am attempting to test my inventory system, for that I need to create a Entity with ItemContainer and fill said container with Entiteis that represent the Items, currently I am attempting to use DynamicBuffers
I sort of know what I am doing wrong, but I have no idea how is the “correct way”.

private void Start() {
        if (World.Active != null) {
            DatabaseLoader.loadDatabase();
            EntityManager em = World.Active.GetOrCreateManager<EntityManager>();
            Entity cont = em.CreateEntity(new ComponentType[] { ComponentType.Create<ItemContainer>() });

            em.AddBuffer<ItemContainerContent>(cont);
            DynamicBuffer<ItemContainerContent> buffer = em.GetBuffer<ItemContainerContent>(cont);

            foreach (KeyValuePair<long, ItemBase> item in ItemProcessor.GetItems()) {
                ItemBaseReference ibr = new ItemBaseReference();
                ibr.ItemReference = item.Key;
                Entity it = em.CreateEntity(new ComponentType[0]);
                em.AddComponentData(it, ibr);
                buffer.Add(it);
            }

            container = cont;
            hasContainer = true;
            UpdateInventory();
        }
    }

With this code, I get the “The NativeArray has been deallocated, it is not allowed to access it” error, because I used the EntityManager to create a Entity or something.
But then comes the question, when and how will I ever be able to fill the DynamicBuffer? I can’t create the buffer and fill it before the Entity, is it impossible to change a DynamicBuffer from outside a System?

Do I have to create a system, whose sole purpose is to, one time only, fill a buffer with debug values?
And I am not even entirely sure how I would go about it

Do your CreateEntity works which cause structural change as much as you like and collect the result in a list, then em.GetBuffer and add them all in one go?

As embarrassing as it sounds, I had completely forgotten that I was creating entities inside the loop, I was under the assumption that the issue was caused by the container entity (cont), not the item (it) entities

This code worked:

if (World.Active != null) {
            DatabaseLoader.loadDatabase();
            EntityManager em = World.Active.GetOrCreateManager<EntityManager>();
            Entity cont = em.CreateEntity(new ComponentType[] { ComponentType.Create<ItemContainer>(), ComponentType.Create<ItemContainerContent>() });

            List<Entity> ents = new List<Entity>();

            foreach (KeyValuePair<long, ItemBase> item in ItemProcessor.GetItems()) {
                ItemBaseReference ibr = new ItemBaseReference();
                ibr.ItemReference = item.Key;
                Entity it = em.CreateEntity(new ComponentType[0]);
                em.AddComponentData(it, ibr);
                ents.Add(it);
            }

            DynamicBuffer<ItemContainerContent> buffer = em.GetBuffer<ItemContainerContent>(cont);
            foreach (Entity it in ents) {
                buffer.Add(it);
            }

            container = cont;
            hasContainer = true;
            UpdateInventory();
        }