help
Root Question Message
public class Category
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public ICollection<Category> Subcategories { get; set; } = new List<Category>();
public int? CategoryId { get; set; }
}
var query = await this.Context()
.Categories
.Include(x => x.Subcategories)
.Where(x => x.Id == 1)
.ToListAsync();
Entity FindParent(int id)
{
var entity = db.Entities.Single(x => x.Id);
if(entity.ParentId is not null)
return FindParent(entity.ParentId.Value);
return null;
}
rightvar dict = await _context.Items.ToDictionaryAsync(x => x);
foreach(var item in dict.Values)
{
if (item.ParentId is not null)
dict[item.ParentId].Children.Add(item);
}
var root = dict.Values.First(x => x.ParentId is null);
_context.Items
.Include(x => x.Children)
.ThenInclude(x => x.Children)
.ThenInclude(x => x.Children)
.ThenInclude(x => x.Children)
InvalidOperationException: Cycle detected while auto-including navigations: 'Category.Subcategories'. To fix this issue, either don't configure at least one navigation in the cycle as auto included in `OnModelCreating` or call 'IgnoreAutoInclude' method on the query.
modelBuilder.Entity<Models.Category>()
.Navigation(e => e.Subcategories)
.AutoInclude();
var query = await this.Context()
.Categories
.Where(x => x.CategoryId == null)
.ToListAsync();
var query = await this.Context()
.Categories
.Include(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
//.Include(x => x.Subcategories)
.AsNoTrackingWithIdentityResolution()
.Where(x => x.CategoryId == null)
.ToListAsync();
public async Task<List<DTO.Category>> ThreadTree(Guid thread)
=> await this.Context()
.Categories
.Include(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.ThenInclude(x => x.Subcategories)
.AsNoTrackingWithIdentityResolution()
.Where(x => x.ThreadId == thread && x.CategoryId == null)
.Select(x => DTO(x))
.ToListAsync();
public static DTO.Category DTO(Models.Category x)
=> new() {
Name = x.Name,
Title = x.Title,
Categories = x.Subcategories.Select(x => DTO(x)).ToList()
};
internal class Category
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public List<Category>? Categories { get; set; }
}
public async Task<List<DTO.Category>> Tree(string thread)
{
var categories = await this.Context()
.Categories
.Include(x => x.Details)
.AsNoTrackingWithIdentityResolution()
.Where(x => x.Thread.Name == thread)
.ToListAsync();
return categories.Where(x => x.CategoryId == null).Select(x => ConvertDTO(x)).ToList();
}
public static DTO.Category ConvertDTO(Models.Category x)
{
var details = x.Details.FirstOrDefault() ?? new();
return new()
{
Name = x.Name,
Culture = details.Culture,
Title = details.Title,
Description = details.Description,
Categories = x.Subcategories.Select(x => ConvertDTO(x)).ToList()
};
}