简体   繁体   English

实体框架代码一个实体的“多对多”

[英]Entity Framework Code First many to many for one entity

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. 我想创建一个新的模型DependencyCategory,像这样来存储多对多子代父关系。

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? 如何在类别模型中与ICollection<DependencyCategory>子代,父代现在创建关系? I want for example when access a category parent to see all children or if is child to see all parent available. 例如,当我访问父类别时,如果要查看所有子项,或者当子目录中的所有父项都可用时,我想要。

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. 我还没有囊括所有内容,因为Code-First不是我的强项,但是它应该为您指明正确的方向并稍微改善您的数据库结构。 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. 请注意, CategoryRelationship类定义了您的关系,在ParentCategoryIdChildCategoryId上都放置了一个复合主键 ,并且您不需要单独的Id列,同时还确保了相同的两个ChildCategoryId不能链接两次。

Hope it helps. 希望能帮助到你。

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM