0

I have model Category:

public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }

I want to create a new model DependencyCategory something like this to store many to many child parent relationship.

public class DependencyCategory
    {
        public int Id { get; set; }
        public Category Child { get; set; }
        public Category Parent { get; set; }
    }

How to create now relationship with ICollection<DependencyCategory> Children, Parent in Category model? I want for example when access a category parent to see all children or if is child to see all parent available.

2 Answers2

0

Based on what I understand, your classes should be defined like this.

public class Category
{
    public int Id {get; set; }
    public string Name {get; set; }
    public string Description {get; set; }

    public List<Category> ParentCategories {get; set; }
    public List<Category> ChildCategories {get; set; }
}

public class CategoryRelationships
{
    public int ParentCategoryId {get; set; }
    public int ChildCategoryId {get; set; }
}

I haven't included all the plumbing as Code-First isn't my strong point, but it should point you in the right direction and improve your database structure slightly. Note the CategoryRelationship class defines your relationships, put a composite-primary-key on both ParentCategoryId and ChildCategoryId and you have no need for a separate Id column, whilst also ensuring that the same two Categories cannot be linked twice.

Hope it helps.

Jon Bellamy
  • 3,333
  • 20
  • 23
  • Ok is the good way but now first how to set the name of new table and name of columns because their are automatically generated?And how to avoid circular reference? – user2297114 Feb 19 '14 at 15:13
  • @user2297114 I'm sorry, I'm not entirely sure what you mean. What is automatically generated, and what circular references need to be avoided? – Jon Bellamy Feb 19 '14 at 15:19
  • I used only Category model and generate table CategoryCategories with column Category_Id Category_Id1. And in this table if I have something like this [5 6] [5 4] [4 6 ]when get a category I have circular references. For childs I have parent also parent have childs. I want to avoid this. – user2297114 Feb 19 '14 at 15:22
  • @user2297114 I'm still not sure I understand - sorry. For my understanding, how does the model work? Is it that a Category can only have a single Child Category, but many Parent Categories, or can a Category have many Child and many Parent Categories? – Jon Bellamy Feb 19 '14 at 16:56
  • A category can have multiple parents and also can have multiple childs. When I added migration only with Category model class EF automatically generate a table in Data Base, with two columns. I don't know how to create this table with a given name and also to set the columns name. – user2297114 Feb 19 '14 at 17:03
  • @user2297114 OK, I understand thanks. I'm not sure I can be of much more help! – Jon Bellamy Feb 19 '14 at 17:17
0

This solved

HasRequired(a => a.ParentProduct)
                .WithMany(b => b.ChildProducts)
                .HasForeignKey(c => c.ParentId) // FK_RelatedProductParentID
                .WillCascadeOnDelete(false);
HasRequired(a => a.ChildProduct)
                .WithMany(b => b.ParentProducts)
                .HasForeignKey(c => c.ChildId); // FK_RelatedProductChildID
public class CategoryDependency
    {
        [Key, Column(Order = 0)]
        public int ParentId { get; set; } // ParentID
        [Key, Column(Order = 1)]
        public int ChildId { get; set; } // ChildID

        // Foreign keys
        public virtual Product ParentProduct { get; set; } //  FK_RelatedProductParentID
        public virtual Product ChildProduct { get; set; } //  FK_RelatedProductChildID
    }
public class Product
    {
        [Key]
        public int ProductId { get; set; } // ProductID (Primary key)
        public string ProductName { get; set; } // ProductName


        // Reverse navigation
        public virtual ICollection<RelatedProduct> ParentProducts { get; set; } // RelatedProduct.FK_RelatedProductChildID
        public virtual ICollection<RelatedProduct> ChildProducts { get; set; } // RelatedProduct.FK_RelatedProductParentID
    }
Community
  • 1
  • 1