0

On a blog page, Subcategories will be linked to the main Category and will be articles of subcategories. Do you think the model structure is correct?

I will develop it with Asp.net Core and EntityFrameworkCore architecture.

public class Category
{
    public int Id { get; set; }

    public string CategoryName { get; set; }


    public bool IsActive { get; set; }

    public ICollection<SubCategory> SubCategories { get; set; }
}
--------------------------------------
public class SubCategory
{
    public int Id { get; set; }

    public string SubCategoryName { get; set; }


    public bool IsActive { get; set; }

    public int CategoryId { get; set; }

    public virtual Category Category { get; set; }

    public virtual ICollection<Article> Articles { get; set; }
}
----------------------------------------------
public class Article
{
    public int Id { get; set;}

    public string ArticleTitle { get; set; }

    public string ArticleContent { get; set; }

    public DateTime PublishDate { get; set; }

    public int AuthorId { get; set; }
    public virtual Author Author { get; set; }

    public int SubCategoryId { get; set; }
    public virtual SubCategory SubCategory { get; set; }
}

1 Answers1

1

Another way is changing the category model to Self-referencing relationship

These links are helpful for Self-referencing 1 , 2

public class Category
{
    public int Id { get; set; }

    public string CategoryName { get; set; }
    public int? ParentId { get; set; }

    public bool IsActive { get; set; }
    public Category Parent { get; set; }
    public ICollection<Category> SubCategories { get; set; }
    public ICollection<ArticleCategories> ArticleCategories { get; set; }
}

then create Many-to-Many relationship between Article and Category class

public class ArticleCategories
{
    public int ArticleId { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
    public Article Article { get; set; }
}

Article model

public class Article
{
    public int Id { get; set;}

    public string ArticleTitle { get; set; }

    public string ArticleContent { get; set; }

    public DateTime PublishDate { get; set; }

    public int AuthorId { get; set; }
    public virtual Author Author { get; set; }

    public ICollection<ArticleCategories> ArticleCategories { get; set; }
}

in this case any Article can have many Category and any Category can used for many Articles

Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41