如何使用Entity Framework 4从父集合中删除子实体? [英] How do I delete a child entity from a parent collection with Entity Framework 4?

查看:118
本文介绍了如何使用Entity Framework 4从父集合中删除子实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 4,并且在父和子实体之间具有一对多的关系。我试图删除一个孩子使用父存储库从父母的子集合中删除:

I'm using Entity Framework 4 and have a one-to-many relationship between a parent and child entity. I'm trying to delete a child using the parent repository by removing it from the parent's children collection:

public virtual void RemoveChild(Child child)
        {
            children.Remove(child);
        }

当我尝试保存更改时,会收到以下错误:

When I try to save the changes I get the following error:


来自ParentChild
AssociationSet的关系位于已删除
状态。
给定多重约束,相应的Child也必须在
'Deleted'状态。

A relationship from the 'ParentChild' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'Child' must also in the 'Deleted' state.

当然我不必使用子存储库明确删除子实体!

Surely I don't have to delete the child entity explicitly using a child repository!

推荐答案

这取决于你是否有级联DB。如果你这样做(而且给你的问题,你可能应该),那么这应该是自动的。 你可以在这里阅读这里

It depends on whether you have a cascade in the DB. If you do (and, given your question, you probably should), then this should be automatic. You can read about this here.

这篇关于如何使用Entity Framework 4从父集合中删除子实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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