MongoDB C# Driver | EF core update nested tree view

I’m encountering an issue when attempting to add nested children to the FolderStructure property of a Project document. I’m using the MongoDB C# Driver in combination with EF Core via a Mongo-compatible provider.

While updates to the FolderStructure at the top-level hierarchy succeed, attempts to insert or update nested folders fail silently or have no effect. It appears the EF Core MongoDB provider only supports updates on top-level properties and does not handle deeply nested structures properly.

I’m looking for a way to reliably update nested folders within the FolderStructure hierarchy using either raw MongoDB driver queries or a compatible EF Core approach.

My classes:

public class Project
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId ProjectId { get; set; }
    public List<TreeFolder> FolderStructure { get; set; } = new List<TreeFolder>();

    [Required]
    public int CompanyId { get; set; }
}

public class TreeFolder
{
    [Required]
    public string Id { get; set; }
    [Required]
    public string Label { get; set; }

    public List<TreeFolder> Children { get; set; } = new List<TreeFolder>();
    
    public List<TreeFile> Files { get; set; } = new List<TreeFile>();
}

Updating the project folder structure by using MongoDB driver:

Project? projectFound = await _context.Projects.FirstOrDefaultAsync(p => p.ProjectId.ToString() == projectId);
List<TreeFolder> folderStructure = ...

var filter = Builders<Project>.Filter.Eq(p => p.ProjectId, ObjectId.Parse(projectId));
var update = Builders<Project>.Update.Set(p => p.FolderStructure, folderStructure);
await _projects.UpdateOneAsync(filter, update);

Updating the project folder structure by using EF Core MongoDB:

projectFound.FolderStructure = folderStructure;
await _context.SaveChangesAsync();

Does anyone have a suggestion for how to persist changes to deeply nested TreeFolder children? The two approaches I used above are not working.

Hi Cad.

Presumably you want the tree folders and files to exist in the Bson document for Project?

I notice that you also have an Id on the Tree Folder. Are you expecting to be able to enforce uniqueness on that?

Hi Cad.

A quick update: I’ve just checked and EF Core itself does not support this recursive nesting.

If you can cope with a limited depth levels you can seemingly get away with it like this:

class RootEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public List<ChildEntityL1> Children { get; set; }
}

abstract class ChildEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

class ChildEntityL1 : ChildEntity
{
    public List<ChildEntityL2> Children { get; set; }
}

class ChildEntityL2 : ChildEntity
{
    public List<ChildEntityL3> Children { get; set; }
}

class ChildEntityL3 : ChildEntity
{
    public List<ChildEntityL4> Children { get; set; }
}

class ChildEntityL4 : ChildEntity
{
    public List<ChildEntityL5> Children { get; set; }
}

class ChildEntityL5 : ChildEntity
{
}

Also bear in mind that while you have “Id” on these child elements you can’t enforce uniqueness on them or use them as keys.

1 Like