且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不在删除工作级联?

更新时间:2023-02-03 07:53:44

我不打扰EF级联删除。我使用数据库管理工具(如MS SQL Server Management Studio)在父表中的父记录上设置级联删除,并让数据库引擎在我使用删除到EF中的父
记录时执行删除。

Hello,

I have the following code:

            Node node = Project.Nodes.Where(n => n.Description.Contains("Polymer Blending Header [Revalidated from PMA Expansion HAZOP (March 2017)]")).First();
            Deviation dev = node.Deviations.Where(d => d.DeviationId == 9158).First();
            Cause cause = dev.Causes.Where(c => c.CauseId == 19673).First();
            Consequence consequence = cause.Consequences.Where(cq => cq.ConsequenceId == 23822).First();
            Recommendation recommendation = consequence.Recommendations.Where(rec => rec.RecommendationId == 4459).First();
            Safeguard safeguard = consequence.Safeguards.Where(sg => sg.SafeguardId == 26988).First();
            Drawing drawing = recommendation.Drawings.Where(dw => dw.DrawingId == 3335).First();
            Remark remark = consequence.Remarks.FirstOrDefault();

            List<Node> nodes = Project.Nodes.ToList();
            foreach (Node n in nodes)
            {
                Context.Entry(n).State = EntityState.Deleted;
            }

            EntityState d_es = Context.Entry(dev).State;
            EntityState c_es = Context.Entry(cause).State;
            EntityState cq_es = Context.Entry(consequence).State;
            EntityState sg_es = Context.Entry(safeguard).State;
            EntityState r_es = Context.Entry(recommendation).State;
            EntityState rm_es = Context.Entry(remark).State;
            EntityState dw_es = Context.Entry(drawing).State;




This code is supposed to delete a whole bunch of entities. These entities are:

* Nodes
* Deviations
* Causes
* Consequences
* Recommendations
* Safeguards
* Remarks
* Drawings

These entities form a hierarchy. Nodes are at the top. Deviations have a foreign key reference to Node, putting them directly under nodes. Causes are directly under deviations (they have a FK reference to them). And so on and so forth for all the other entities. The only time this patterns breaks is for Recommendations, Safeguards, and Remarks, all three of which are direct children to Consequences. Drawings are direct children of Recommendations.

In the EDMX model, I set all relation to cascade on delete. What this means is that in order to delete everything, I should only have to delete Nodes. But when I run the code above (the foreach loop in particular), this is not what happens. All nodes are marked as deleted, all deviations are marked as modified, and all other entities are marked as unchanged.

Why isn't the cascade setting working?

Here's an example of how I set cascading on delete for the relation between nodes and deviations:



I don't bother with EF cascade deletes. I set cascade delete on the parent record in the parent table using the database administration tool like MS SQL Server Management Studio and let the database engine do the deletes when I use the delete to the parent record in EF.