public async Task SetParentAsync(TEntity entity, IMaterializedPathEntity <TId>?parent) { AssertIsStoredEntity(entity); if (parent is not null) { AssertIsStoredEntity(parent !); } var path = ParsePath(parent?.Path); if (parent is not null) { path.Add(parent.Id); } var oldPath = entity.Path; var newPath = FormatPath(path); foreach (var descendant in await QueryDescendants(entity).ToListAsync()) { var newDescendantPath = ParsePath(descendant.Path.Replace(oldPath, newPath)); descendant.Path = FormatPath(newDescendantPath); descendant.Level = newDescendantPath.Count; } entity.Level = path.Count; entity.Path = newPath; entity.ParentId = parent?.Id; await dbContext.SaveChangesAsync(); }
private static void AssertIsStoredEntity(IMaterializedPathEntity <TId> entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (entity.Id.Equals(default(TId))) { throw new InvalidOperationException($"{nameof(entity)} does not have valid Id. Save this entity first."); } }