Delete Owned Entities 2 level depth using GraphDiff

c# ef-code-first entity-framework graphdiff

Question

In my MVC project, I'm using GraphDiff and the Entity Framework Code First (version 6) framework.

Here are several entities that correspond to certain database tables.

public class CommunicationPlan 
{
    public int CommunicationPlanID { get; private set; }

    [Owned]
    public List<CommunicationTopic> Topics { get; private set; }
}

public class CommunicationTopic 
{
    public int CommunicationTopicID { get; private set; }

    [Owned]
    public List<ContributingMember> Members { get; private set; }
}

public class ContributingMember
{
    public int ContributingMemberID { get; private set; }

    // other simple properties
}

When I produceCommunicationPlan which numerousCommunicationTopic s and theirContributingMember Save the aggregate root and the sCommunicationPlan , the GraphDiff will produce each record and link it to its corresponding entry in the database. (Exactly as I want it)

Problem
When I attempt to get rid of one of theCommunicationTopic from currentCommunicationPlan then, when needed, this subject is removed from the database, butContributingMember which were connected to thatCommunicationTopic does NOT delete from the database; rather, their Foreign Key values are set to be null, but they still exist.

As soon as I set up the Foreign key of theContributingMember if you want to make it reject null values, I get the following error

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

Question How can I set up the Entities such that they cause GraphDiff to delete?ContributingMember As soon as its parent -CommunicationTopic - removed?

1
1
8/25/2016 11:33:11 AM

Accepted Answer

I believe there may be two solutions to this problem.

  1. Try setting the database relationship's cascading attribute to CASCADE DELETE between CommunicationTopic and ContributingMember.
  2. When attempting to remove your AggregateRoot, ensure that the whole graph is loaded.
1
8/27/2016 8:56:02 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow