为什么不在删除工作级联? [英] Why doesn't cascade on delete work?

查看:74
本文介绍了为什么不在删除工作级联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有以下代码:



           节点node = Project.Nodes.Where(n => n.Description.Contains(" Polymer Blending Header [从PMA Expansion HAZOP重新验证(2017年3月)]"))。First(); 
            Deviation dev = node.Deviations.Where(d => d.DeviationId == 9158).First();
           原因= dev.Causes.Where(c => c.CauseId == 19673).First();
           结果后果= cause.Consequences.Where(cq => cq.ConsequenceId == 23822).First();
           推荐建议=结果。建议。其中(rec => rec.RecommendationId == 4459).First();
            Safeguard safeguard = outcome.Safeguards.Where(sg => sg.SafeguardId == 26988).First();
           绘图= vote.Drawings.Where(dw => dw.DrawingId == 3335).First();
           备注remark = suff.Remarks.FirstOrDefault();

           列表与LT;节点> nodes = Project.Nodes.ToList();
            foreach(节点中的节点n)
            {
                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(后果).State;
            EntityState sg_es = Context.Entry(safeguard).State;
            EntityState r_es = Context.Entry(推荐).State;
            EntityState rm_es = Context.Entry(remark).State;
            EntityState dw_es = Context.Entry(drawing).State;








此代码为应该删除一大堆实体。这些实体是:



*节点

*偏差

*原因

*后果

*建议

*保障措施

*备注

*图纸



这些实体构成一个层次结构。节点位于顶部。偏差具有对Node的外键引用,将它们直接放在节点下。原因直接在偏差之下(他们有FK参考)。等等所有其他实体等等。
此模式中断的唯一时间是建议书,保障措施和备注,这三个都是直接子项到后果。图纸是建议书的直接子项。



在EDMX模型中,我将所有关系设置为删除级联。这意味着为了删除所有内容,我只需要删除节点。但是当我运行上面的代码(特别是foreach循环)时,这不是发生的事情。所有节点都标记为
已删除,所有偏差都标记为已修改,所有其他实体都标记为未更改。



为什么不是级联设置工作?
$


这里有一个例子,说明我如何在删除时为节点和偏差之间的关系设置级联:






解决方案

我不打扰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.


这篇关于为什么不在删除工作级联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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