Categories/Subcategories in asp.net mvc

前端 未结 3 1010
慢半拍i
慢半拍i 2020-12-30 15:12

We are making a marketplace like https://www.etsy.com/. And we have a problem in categorising the listings. We want to categories the item in the Listing in 3 levels, f.ex i

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 15:49

    You should absolutely not have multiple category/subcategory entities. A category can have a parent and it can have children, but they're all "categories".

    public class Category
    {
        public int Id { get; set; }
    
        public int? ParentId { get; set; }
        public virtual Category Parent { get; set; }
    
        public virtual ICollection Children { get; set; }
    }
    

    ParentId is nullable, because top-level categories have no parent.

    Entity Framework tends to get confused by self-referencing relationships, so you might need a little fluent config to help it out:

    public class Category
    {
        // properties
    
        public class Mapping : EntityTypeConfiguration
        {
            public class Mapping()
            {
                HasOptional(m => m.Parent).WithMany(m => m.Children);
            }
        }
    }
    

    Then, in your context:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new Category.Mapping());
    }
    

    With all that in place, when you're in your "Electronics" category, you'd show the subcategories simply by iterating over it's Children property.

    UPDATE

    If you need the full hierarchy rather than just one level at a time, you have a couple of options. First, you can just include multiple levels when querying:

    db.Categories.Include("Children.Children");
    

    That's not highly efficient, though, and I definitely would not recommend delving much deeper than tertiary children. However, that's all you're asking for, so this is still a workable method.

    Second, you can create a stored procedure to walk the hierarchical structure for you. It's a little more complex, but with a combination of WITH and UNION ALL, you can create a flat representation of the hierarchy and then recursively use LINQ's GroupBy to work it back into a hierarchical structure.

    There's a final potential third option in @Hackerman's recommendation of using HIERARCHYID, but unfortunately, to do that, you must completely remove Category from your EF context, which also means removing any direct relationships to it, as well. To relate a product to a category, you could only store the id (not as a foreign key), and then use that id to manually lookup the category in a second step. Unfortunately, while this solution makes dealing the the hierarchy easier, it makes doing everything else more difficult. Either way, it's up to you, though.

提交回复
热议问题