简体   繁体   English

实体框架中的级联删除

[英]Cascading delete in Entity Framework

So I have two tables, Invoices and InvoiceItems. 所以我有两个表,Invoices和InvoiceItems。 When I delete an Invoice, I'd like all the related InvoiceItems to be deleted as well. 当我删除发票时,我也希望删除所有相关的InvoiceItems。

I updated the relationship in SQL Server to do a cascading delete when I delete an Invoice. 我更新了SQL Server中的关系,以便在删除发票时进行级联删除。 Entity Framework didn't recognize that change, however, but I've read that I need to manually update my EDMX to do the cascading delete. 然而,实体框架没有认识到这种改变,但我已经读过我需要手动更新我的EDMX以进行级联删除。

Well in the design view of my EDMX, I clicked on the relationship between the two tables, and checked the properties to try and set my cascading delete 在我的EDMX的设计视图中,我点击了两个表之间的关系,并检查了属性以尝试设置我的级联删除

在此输入图像描述

As you can see, there are two OnDelete properties: End1 OnDelete and End2 OnDelete 如您所见,有两个OnDelete属性:End1 OnDelete和End2 OnDelete

Which one do I need to set to Cascade? 我需要将哪一个设置为Cascade?

I was initially confused about this as well, and the reason was that I wasn't sure of what whether it was the field Navigation Property or the field Role Name that described what object an end represented, making an answer as Devidigitals ineffective. 我最初也对此感到困惑,原因是我不确定它是字段导航属性还是字段角色名称描述了一个结尾所表示的对象,作为Devidigitals无效的答案。

And the answer to that is that a end is described by the field role name . 答案就是字段角色名称描述了结束。

So if we know that we want to delete all InvoiceItems for a Invoice when the invoice in question gets deleted, we probably know that we should set the OnDelete for the Invoice , and to find the specific end that represents the invoice in the relation, search for the end where the role name is "Invoice". 因此,如果我们知道在有问题的发票被删除时我们想要删除发票的所有InvoiceItems ,我们可能知道我们应该为发票设置OnDelete,并找到代表关系中发票的特定结尾,搜索在角色名称为“Invoice”的末尾。 In the above example, that is End1. 在上面的例子中,那就是End1。

This might be obvious if you know it, but not if you don't. 如果您知道这一点,这可能是显而易见的,但如果您不知道则不是。

如果End1是关系的主体(即您的发票上有发票项目),那么它是有意义的级联删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM