2
public class Category 
{ 
    [Key] 
    public int CategoryId { get; set; } 
    public int ParentCategoryId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public int Status { get; set; } 
    public virtual ICollection<Category> ParentCategories { get; set; } 
    public virtual ICollection<ImageSet> ImageSets { get; set; } 

    [ForeignKey("ParentCategoryId")] 
    public virtual Category ParentCategory { get; set; } 
}  

public class ImageSet 
{ 
    [Key] 
    public int ImageSetId { get; set; } 
    public int CategoryId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string InsertDate { get; set; } 
    public int Status { get; set; } 
    public virtual ICollection<Image> Images { get; set; } 

    [ForeignKey("CategoryId")] 
    public virtual Category Category { get; set; } 
} 

public class Image 
{ 
    [Key] 
    public int ImageId { get; set; } 
    public int ImageSetId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string ImageUrl { get; set; } 
    public string ThumbImageUrl { get; set; } 
    public string InsertDate { get; set; } 
    public int Status { get; set; } 

    [ForeignKey("ImageSetId")] 
    public virtual ImageSet ImageSet { get; set; } 
} 

Context: 
    public DbSet<Category> Categories { get; set; } 
    public DbSet<Image> Images { get; set; } 
    public DbSet<ImageSet> ImageSets { get; set; } 

錯誤頁:在表將外鍵約束「FK_dbo.ImageSets_dbo.Categories_CategoryId」「ImageSets」可以 原因循環或多個級聯路徑。指定ON DELETE NO ACTION或 ON UPDATE NO ACTION,或修改其他FOREIGN KEY約束。 可能不會創建約束。查看以前的錯誤。代碼優先一對多的關係和類別與父類別

最新問題?

回答

2

你需要補充一點:

modelBuilder.Entity<ImageSet>() 
.HasRequired(is => is.Category) 
.WithMany(c => c.ImageSets) 
.WillCascadeOnDelete(false); 

這裏是爲什麼發生這種情況的好解釋:

https://stackoverflow.com/a/19390016/1845408

https://stackoverflow.com/a/17127512/1845408

+0

由於其工作。 –

+0

很高興幫助兄弟;)@RamazanSağır – renakre

+0

Eyvallah hocam,安拉razıolsun ...Şumecraya daTürkçeserpiştirelimbiraz。 :d –