1

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 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks sir, this solved my problem. I just forgot to set the value of the foreign key of the ContributingMember it was equal to 0, so when updating the database with the foreign key 0, I was receiving update conflict exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.