EF 在同一 table 上与另一个属性具有多对多关系

EF with many to many relation on the same table with another attributes

我无法首先使用 ef 代码在同一个 table 上建立多对多关系。有没有人可以帮助我? 我也有一个类别 class 和一个类别关系 class。类别 class 有许多父类别和许多子类别。但是我在 CategoryRelation 中有一个额外的道具作为 DisplayId。我的class如下

类别:

 public class Category : Base
{
    #region Top Menu
    public int DisplayOrder { get; set; }
    public bool ShowOnTopMenu { get; set; }
    #endregion

    public bool ShowOnTopSelling { get; set; }
    [MaxLength(100)]
    public string Name { get; set; }

    #region Types
    public bool IsPieceType { get; set; }
    public bool IsGildingType { get; set; }
    public bool IsPaperType { get; set; }
    public bool IsSizeType { get; set; }
    public bool IsWeightType { get; set; }
    public bool IsCellophaneType { get; set; }
    public bool IsCuttingType { get; set; }
    public bool IsPrintingType { get; set; }
    #endregion

    public virtual ICollection<CategoryRelation> ChildCategoryList { get; set; }    
    public virtual ICollection<CategoryRelation> ParentCategoryList { get; set; }
    public virtual ICollection<Product> ProductList { get; set; }
    public virtual ICollection<WishList> WishList { get; set; }
    public virtual ICollection<Description> DescriptionList { get; set; }
    public virtual ICollection<Comment> CommentList { get; set; }
    public virtual ICollection<Image> ImageList { get; set; }
    public virtual ICollection<Template> TemplateList { get; set; }
    public virtual ICollection<PromotionCode> PromotionCodeList { get; set; }
}

类别关系;

public class CategoryReletion : Base
    {
        #region Parent Category
        [ForeignKey("ParentCategory")]
        public int ParentId { get; set; }
        public Category ParentCategory { get; set; }
        #endregion

        #region Child Category
        [ForeignKey("ChildCategory")]
        public int ChildId { get; set; }
        public Category ChildCategory { get; set; }
        #endregion
        public int DisplayOrder { get; set; }
    }

我终于收到这个错误了;

Introducing FOREIGN KEY constraint 'FK_dbo.CategoryReletion_dbo.Category_ParentId' on table 'CategoryReletion' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.*

你可以完全按照你说的去做(我假设你的上下文是名字MyContext

public class MyContext: DbContext
{
    public DbSet<Category> Categories { get; set; }
    public DbSet<CategoryReletion> CategoryReletions { get; set; }

      protected override void OnModelCreating( DbModelBuilder modelBuilder )
      {
         modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
         modelBuilder.Entity<CategoryReletion>()
            .HasRequired( c => c.ParentId)
            .WithRequiredDependent()
            .WillCascadeOnDelete(false);

         modelBuilder.Entity<CategoryReletion>()
            .HasRequired( c => c.ChildId)
            .WithRequiredDependent()
            .WillCascadeOnDelete(false);
      }

}

发生这种情况是因为存在多个 many to many 关系。

并且您有 cascadeDelete On,这意味着如果您删除一行,所有相关行都将被删除,想象一下这在多个关系上是如何工作的,您可能会以删除行的删除顺序结束。

所以为了解决这个问题你需要使用 FluentApi 关闭 CascadeDelete

它适用于这些代码;

       modelBuilder.Entity<CategoryRelation>()
              .HasKey(c => new { c.ParentId, c.ChildId });

        modelBuilder.Entity<CategoryRelation>()
              .HasRequired(c => c.ParentCategory)
              .WithMany(c => c.ChildCategoryList)
              .HasForeignKey(c => c.ParentId)
              .WillCascadeOnDelete(false);

        modelBuilder.Entity<CategoryRelation>()
            .HasRequired(c => c.ChildCategory)
            .WithMany(c => c.ParentCategoryList)
            .HasForeignKey(c => c.ChildId)
            .WillCascadeOnDelete(false);