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

我不能先使用ef代码在同一张表上建立多对多关系。有没有人可以帮助我? 我也有一个Category类和CategoryRelation类。 Categoy类具有许多父类别和许多子类别。但是我在CategoryRelation中有一个额外的道具作为DisplayId。我的课程就像下面的

类别:

 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; }
}

CategoryRelation;

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; }
    }

最后我遇到这个错误;

在表“ CategoryReletion”上引入FOREIGN KEY约束“ FK_dbo.CategoryReletion_dbo.Category_ParentId”可能会导致循环或多个级联路径。指定ON DELETE NO actION或ON UPDATE NO actION,或修改其他FOREIGN KEY约束。 无法创建约束或索引。请参阅先前的错误。*

huanghongwang123 回答:具有同一属性的EF与多对多关系在另一个表上

它适用于这些代码;

       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);
,

您可以按照提示进行操作(我假设您的上下文名称为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

本文链接:https://www.f2er.com/2714317.html

大家都在问