How to disable cascade delete for link tables in EF code-first?

前端 未结 3 1430
说谎
说谎 2020-11-27 17:57

I want to disable cascade deletes for a link table with entity framework code-first. For example, if many users have many roles, and I try to delete a role, I want that del

3条回答
  •  星月不相逢
    2020-11-27 18:11

    I believe that turning off ManyToManyCascadeDeleteConvention globally is not a wise option. Instead, it's better to turn it off only for the concerned table.

    This can be achieved through editing the generated migration file, for property cascadeDelete. For example:

    AddForeignKey("dbo.UsersRoles", "UserId", "dbo.User", "UserId", cascadeDelete: false);

提交回复
热议问题