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.