Entity Framework 4.1 Codefirst: “Given multiplicity constraints” error when deleting one-to-many children

前端 未结 2 643
星月不相逢
星月不相逢 2021-01-18 01:36

I have the following classes in Entity Framework 4.1 (the classes have been pruned to keep the code readable)

public class MetaInformation
{         


        
2条回答
  •  离开以前
    2021-01-18 02:42

    Cascade delete will not help you here. Cascade delete works only if you delete parent entity (MetaInformationObject). The problem here is that calling Clear on the collection of related objects doesn't delete them. It only deletes the relation (= it will set foreign key in MetaInformation to null) but it is not allowed by your mapping constraints (you defined relation as required). The ways to avoid this are either:

    • Iterate through all related MetaInformations and delete each of them
    • Refactor you model to define identifying relation. After that Clear will not only break the relation but mark related entity as deleted as well.

提交回复
热议问题