Key: -1 is not Present in the NativeParallelHashMap

I’m getting the following Error:

System.ArgumentException: Key: -1 is not present in the NativeParallelHashMap.
This Exception was thrown from a job compiled with Burst, which has limited exception support.
0x00007ffd0cdb9cfe (Unity) burst_abort
0x00007ffd3cf9f834 (a085deb0d515f05d108a9a551d2afec) burst_Abort_Trampoline
0x00007ffd3cefa063 (a085deb0d515f05d108a9a551d2afec) Unity.Collections.NativeParallelHashMap2<int,UnityEngine.Rendering.BatchFilterSettings>.ThrowKeyNotPresent (at C:/Users/Adrian/Documents/Coding/DOTS/Library/PackageCache/com.unity.burst/.Runtime/Library/PackageCache/com.unity.collections/Unity.Collections/NativeParallelHashMap.cs:822) 0x00007ffd3cefa0ef (a085deb0d515f05d108a9a551d2afec) Unity.Collections.NativeParallelHashMap2<int,UnityEngine.Rendering.BatchFilterSettings>.get_Item (at C:/Users/Adrian/Documents/Coding/DOTS/Library/PackageCache/com.unity.burst/.Runtime/Library/PackageCache/com.unity.collections/Unity.Collections/NativeParallelHashMap.cs:268)
0x00007ffd3cf8da27 (a085deb0d515f05d108a9a551d2afec) Unity.Rendering.EmitDrawCommandsJob.Execute (at C:/Users/Adrian/Documents/Coding/DOTS/Library/PackageCache/com.unity.burst/.Runtime/Library/PackageCache/com.unity.entities.graphics/Unity.Entities.Graphics/DrawCommandGeneration.cs:1045)
0x00007ffd3cf8ed52 (a085deb0d515f05d108a9a551d2afec) Unity.Jobs.IJobParallelForDeferExtensions.JobParallelForDeferProducer`1<Unity.Rendering.EmitDrawCommandsJob>.Execute(ref Unity.Rendering.EmitDrawCommandsJob jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex) → void_367ef9ef715f30bf3e650f3b9451911b from Unity.Collections, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null (at C:/Users/Adrian/Documents/Coding/DOTS/Library/PackageCache/com.unity.burst/.Runtime/Library/PackageCache/com.unity.collections/Unity.Collections/Jobs/IJobParallelForDefer.cs:78)
0x00007ffd3cf89f87 (a085deb0d515f05d108a9a551d2afec) e69356d14e4c3ba60caed2c69a54e52e
0x00007ffd0cdb63d5 (Unity) ExecuteJob
0x00007ffd0cdb66fe (Unity) ExecuteJobCopyData
0x00007ffd0cdb3926 (Unity) ujob_execute_job
0x00007ffd0cdb2adf (Unity) lane_guts
0x00007ffd0cdb5724 (Unity) worker_thread_routine
0x00007ffd0cfabedd (Unity) Thread::RunThreadWrapper
0x00007ffdb2ac7374 (KERNEL32) BaseThreadInitThunk
0x00007ffdb467cc91 (ntdll) RtlUserThreadStart

This is the code that caused this. If I comment this whole System, no error gets thrown:

using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using UnityEngine.Rendering;

public partial class ChunkSystem2 : SystemBase
{
    private EntitiesGraphicsSystem _entitiesGraphicsSystem;
    private EntityCommandBuffer entityCommandBuffer;
    private EndSimulationEntityCommandBufferSystem _endSimulationEcbSystem;

    protected override void OnCreate()
    {
        RequireForUpdate<MeshComponent>();

        _endSimulationEcbSystem = World.GetOrCreateSystemManaged<EndSimulationEntityCommandBufferSystem>();
        _entitiesGraphicsSystem = World.GetOrCreateSystemManaged<EntitiesGraphicsSystem>();
    }

    protected override void OnUpdate()
    {
        var ecb = _endSimulationEcbSystem.CreateCommandBuffer().AsParallelWriter();

        Entities.ForEach((Entity entity, int entityInQueryIndex, in Chunk chunk, in MeshComponent meshComponent, in LocalTransform transform) =>
        {
            // Parallel-safe operations here, for example:
            var mesh = new Mesh
            {
                vertices = meshComponent.MeshData.Value.Vertices.ToArray(),
                triangles = meshComponent.MeshData.Value.Triangles.ToArray(),
                uv = meshComponent.MeshData.Value.UVs.ToArray()
            };

            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            BatchMeshID meshID = _entitiesGraphicsSystem.RegisterMesh(mesh);

            if (meshID.Equals(BatchMeshID.Null))
            {
                Debug.LogError("Failed to register mesh.");
                return;
            }

            if (chunk.material < 0)
            {
                Debug.LogError("Invalid material index.");
                return;
            }

            BatchMaterialID materialID = _entitiesGraphicsSystem.RegisterMaterial(MaterialManager.GetMaterialByIndex(chunk.material));
            if (materialID.Equals(BatchMaterialID.Null))
            {
                Debug.LogError("Failed to register material.");
                return;
            }

            ecb.AddComponent(entityInQueryIndex, entity, new MaterialMeshInfo
            {
                MeshID = meshID,
                MaterialID = materialID
            });

            ecb.AddComponent(entityInQueryIndex, entity, new RenderBounds
            {
                Value = mesh.bounds.ToAABB()
            });

            ecb.AddComponent<RenderMeshUnmanaged>(entityInQueryIndex, entity, new RenderMeshUnmanaged
            {
                mesh = mesh
            });

            ecb.RemoveComponent<Chunk>(entityInQueryIndex, entity);
        }).WithoutBurst().Run();
    }
}

You’re missing the RenderFilterSettings shared component. Please consult this page which lists required components.

With that said, it is recommended to use a base prefab (baked, or created at runtime with RenderMeshUtility) and instantiate copies in the command buffer, wherever applicable.