Delete Owned Entities 2 level depth using GraphDiff

c# ef-code-first entity-framework graphdiff

Question

I am using Entity Framework code first (version 6) and GraphDiff in my MVC project.

here is some entities which map some tables in database.

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 create CommunicationPlan which have many CommunicationTopics with their ContributingMembers and save the aggregate root CommunicationPlan, then the GraphDiff will create all the records and relate them in the database. (as I want exactly)

Problem
When I am trying to delete one of the CommunicationTopic from existing CommunicationPlan then this topic is deleted from the database (as I need), But the ContributingMembers which related to that CommunicationTopic does NOT deleted from database , It is just their Foreign key values set to be null, and they reside in the database.

When I configure the Foreign key of the ContributingMember to make it not accepting null values, then I receive the following exception

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 configure the Entities to make GraphDiff delete ContributingMembers records when its parent -CommunicationTopic- deleted?

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

Accepted Answer

I think you may have two way to fix this issue

  1. Try to configure the cascading property of the relationship in database to be CASCADE DELETE between CommunicationTopic and ContributingMember
  2. Make sure that the whole graph of your AggregateRoot is loaded when you try to delete it
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