实体框架中的WillCascadeOnDelete如何工作? [英] How does WillCascadeOnDelete in Entity Framework work?

查看:156
本文介绍了实体框架中的WillCascadeOnDelete如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,如果删除父行,并且在删除时启用层叠,则应删除其子行。但是,从我的测试来看,它似乎根本不起作用。不管我将WillCascaseOnDelete设置为true还是false,它都只是将其子级的外键设置为null。这导致另一个问题,我必须将外键设置为可空,否则,SaveChange将引发异常。这是缺陷还是期望的行为?

As I understand, if I delete a parent row, its children should be deleted if I turn on cascade on delete. However, from my testing, it doesn't seem to work at all. No matter if I set WillCascaseOnDelete to true or false, it simply sets the foreign key of its children to null. This is causing another problem that I have to set the foreign key nullable, otherwise, SaveChange will throw exception. Is this a defect or desired behavior?

推荐答案

这是因为您的外键(子代)可以为空。默认情况下,删除父级时,如果关系上的外键可为空,则EF会删除父级并将外键设置为null。如果外键不是NOT NULL,它将删除子项(您正在寻找的行为?)。

This is because your foreign keys (child) are nullable. By default, when deleting parent, if the foreign key on the relationship is nullable EF will delete the parent and set the foreign key to null. If the foreign key is NOT NULL it will delete the child (the behaviour you're looking for?).

您可以更改此默认行为此处

You can alter this default behaviour here

这篇关于实体框架中的WillCascadeOnDelete如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆