Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

前端 未结 18 1243
天涯浪人
天涯浪人 2020-11-22 02:37

I\'ve been wrestling with this for a while and can\'t quite figure out what\'s happening. I have a Card entity which contains Sides (usually 2) - and both Cards and Sides h

18条回答
  •  一整个雨季
    2020-11-22 03:21

    public partial class recommended_books : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.RecommendedBook",
                c => new
                    {
                        RecommendedBookID = c.Int(nullable: false, identity: true),
                        CourseID = c.Int(nullable: false),
                        DepartmentID = c.Int(nullable: false),
                        Title = c.String(),
                        Author = c.String(),
                        PublicationDate = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.RecommendedBookID)
                .ForeignKey("dbo.Course", t => t.CourseID, cascadeDelete: false) // was true on migration
                .ForeignKey("dbo.Department", t => t.DepartmentID, cascadeDelete: false) // was true on migration
                .Index(t => t.CourseID)
                .Index(t => t.DepartmentID);
    
        }
    
        public override void Down()
        {
            DropForeignKey("dbo.RecommendedBook", "DepartmentID", "dbo.Department");
            DropForeignKey("dbo.RecommendedBook", "CourseID", "dbo.Course");
            DropIndex("dbo.RecommendedBook", new[] { "DepartmentID" });
            DropIndex("dbo.RecommendedBook", new[] { "CourseID" });
            DropTable("dbo.RecommendedBook");
        }
    }
    

    When your migration fails you are given a couple of options: 'Introducing FOREIGN KEY constraint 'FK_dbo.RecommendedBook_dbo.Department_DepartmentID' on table 'RecommendedBook' 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.'

    Here is an example of using the 'modify other FOREIGN KEY constraints' by setting 'cascadeDelete' to false in the migration file and then run 'update-database'.

提交回复
热议问题