Selectively Disabling Cascade Delete on Many-to-Many Link Table

你。 提交于 2019-12-05 05:47:15

I suspect there is no way to do it in an EntityTypeConfiguration mapping class, but I did it by changing the code in the Up() method in the DbMigration class.

The code generated for the link table is:

CreateTable(
    "dbo.ChildBChildAs",
    c => new
        {
            ChildB_Id = c.Int(nullable: false),
            ChildA_Id = c.Int(nullable: false),
        })
    .PrimaryKey(t => new { t.ChildB_Id, t.ChildA_Id })
    .ForeignKey("dbo.ChildBs", t => t.ChildB_Id, cascadeDelete: true)
    .ForeignKey("dbo.ChildAs", t => t.ChildA_Id, cascadeDelete: true)
    .Index(t => t.ChildB_Id)
    .Index(t => t.ChildA_Id);

You should be able to get it working by changing the side you don't want to cascade to false:

    .ForeignKey("dbo.ChildAs", t => t.ChildA_Id, cascadeDelete: false)

It would be nice if you could do it using the FluentAPI as you can with one-to-many, but I haven't managed to find a way to do it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!