locked
cycles or multiple cascade paths in ef core RRS feed

  • Question

  • User338455301 posted

    Hi,

    i have this error in update database , whats problem ?

    ProductInfo.cs

      [Key]
            public int ProductInfo_ID { get; set; }
    
      [InverseProperty("ProductInfo")]
            public virtual IEnumerable<SimilarProduct> SimilarProduct { get; set; }
    
    
            [InverseProperty("SimilarProduct1")]
            public virtual IEnumerable<SimilarProduct> SimilarProduct2 { get; set; }
    

    SimilarProduct.cs

     [Key]
            public int ID { get; set; }
    
            [ForeignKey("ProductInfo")]
            public int ProductInfo_ID { get; set; }
            public ProductInfo ProductInfo { get; set; }
    
    
            [ForeignKey("SimilarProduct1")]
            public int ProductSimilarID { get; set; }
            public ProductInfo SimilarProduct1 { get; set; }

    Error
    Introducing FOREIGN KEY constraint 'FK_SimilarProduct_tb_ProductInfo_tb_ProductSimilarID' on table 'SimilarProduct_tb' 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.

    i set this but its not worked 

              builder.Entity<SimilarProduct>()       // THIS IS FIRST
    .HasOne(u => u.ProductInfo).WithMany(u => u.SimilarProduct).IsRequired().OnDelete(DeleteBehavior.Restrict);
    
                builder.Entity<SimilarProduct>()
           .HasOne(pt => pt.SimilarProduct1)
           .WithMany(p => p.SimilarProduct2)
           .HasForeignKey(pt => pt.ProductSimilarID).OnDelete(DeleteBehavior.Restrict); 
    



    Monday, April 13, 2020 11:41 AM

All replies

  • User-854763662 posted

    Hi elahi1mahdi ,

    Are you saying that the error occurred when using the update-database command?

    I tested your code , but there is no error and it updated the database successfully in migration.

    Did you try the solution suggested in the error ? What's your generated migration file and the complete DbContext? 

    Best Regards,

    Sherry

    Tuesday, April 14, 2020 6:24 AM
  • User-217098811 posted

    Hi elahi1mahdi

    You have encountered an error while updating the database because you did not remove the previous error migration

    Try entering in the package manager console

    Remove-Migration

    Remember to delete all migrations

    I had the same problem as you and I solved it in this way

    Best Regards

    yinqiu

    Tuesday, June 2, 2020 6:39 AM