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

error page:Introducing FOREIGN KEY constraint 'FK_dbo.ImageSets_dbo.Categories_CategoryId' on table 'ImageSets' 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. See previous errors.

Whats the problem?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Ramazan Sağır
  • 4,887
  • 1
  • 14
  • 15

1 Answers1

2

You need to add this:

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

Here are good explanations of why this is happening :

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

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

Community
  • 1
  • 1
renakre
  • 8,001
  • 5
  • 46
  • 99