C
C#2y ago
Alerin

❔ How to make categories correctly?

I need to make a category in such a way that each can be inherited. I have this model:
public class Category
{
public int Id { get; set; }

public Thread Thread { get; set; } = null!;
public Guid ThreadId { get; set; }
public int Sorting { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public ICollection<CategoryDetail> Details { get; set; } = null!;
public ICollection<Category> Subcategories { get; set; } = new List<Category>();
public int? CategoryId { get; set; }
}
public class Category
{
public int Id { get; set; }

public Thread Thread { get; set; } = null!;
public Guid ThreadId { get; set; }
public int Sorting { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public ICollection<CategoryDetail> Details { get; set; } = null!;
public ICollection<Category> Subcategories { get; set; } = new List<Category>();
public int? CategoryId { get; set; }
}
public class CategoryDetail
{
public int Id { get; set; }
public Category Category { get; set; } = null!;
public int CategoryId { get; set; }

public string Culture { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
public class CategoryDetail
{
public int Id { get; set; }
public Category Category { get; set; } = null!;
public int CategoryId { get; set; }

public string Culture { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
Unfortunately, I'm having a big problem making an efficient query in EF Core. Did I do the model right?
2 Replies
Anton
Anton2y ago
why is there two IDs? generally, the best way to model details is to have every field of every of type of detail be sparse and nullable and in a single table (denormalized). look up some stuff on inheritance performance in efcore, it's related to this problem, they describe the different ways of storing these things making each category be in a separate table (like you did it) is the worst way performance wise albeit the most normalized and logical from the standpoint of relational database theory also, even if you do it your way, you don't need the id of the category detail, you can make it use the id of the category as its own, since there's gonna be a one to one relationship
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server