ivon
ivon

Reputation: 17

Delete object from an entity with many to many relationship

I am new to Entity Framework so I need help with deleting an object from an entity. I have 2 tables which are in many to many relationship and an association table connecting them in the database. In the model there are only two tables and the association one is presented by navigation properties as this is how the EF works. Now I need to delete an object from the first table by context.EntityName.DeleteObject(object) but when I try to do it the code fails with error "The DELETE statement conflicted with the REFERENCE constraint FK..", which is a foreign key from the association table to the entity, which object I try to delete. I wonder how to fix this. Could you please help me?

Here is how the tables look like:

Teachers

TimetableDetail

and the associaion table:

TimetableDetailTeachers

And here is how I try to delete it:

TimetablesEntities context = new TimetablesEntities();

TimetableDetail detail = context.TimetableDetails.SingleOrDefault(td => td.TimetableDetail_ID == timetableDetailId);

context.TimetableDetails.DeleteObject(detail);

context.SaveChanges();

Thanks in advance!

Upvotes: 1

Views: 2185

Answers (3)

user5683877
user5683877

Reputation:

You could do: add row before remove detail:

context.Teachers.RemoveRange(detail.Teachers);

The key line being that...

Upvotes: 0

jimSampica
jimSampica

Reputation: 12410

You just need to clear the association table by clearing the Teachers list for a particular TimetableDetail. Using your code...

TimetablesEntities context = new TimetablesEntities();

TimetableDetail detail = context.TimetableDetails.SingleOrDefault(td => td.TimetableDetail_ID == timetableDetailId);

detail.Teachers.Clear();

context.TimetableDetails.DeleteObject(detail);

context.SaveChanges();

The key line being detail.Teachers.Clear()

Upvotes: 5

László Koller
László Koller

Reputation: 1159

Yeah, this is a tricky one.

What you need to do is clear the entities out of the EF's underlying local storage. The example shows what to do when you clear all details for a specific teacher (or even only some details) and then save that teacher's entity. With that in mind, here is some example repository code:

public void EditTeacher(Teacher teacher)
{
    if (teacher == null)
    {
        throw new ArgumentNullException("teacher");
    }

    YourDbContext.Entry(teacher).State = EntityState.Modified;

    // Remove all timetable details that have an orphaned relationship.
    // (E.g., orphaning occurs when 'teacher.TimetableDetails.Clear()'
    //  is called or when you delete one particular TimetableDetail
    //  entity for a teacher)
    YourDbContext.TimetableDetails
        .Local
        .Where(td => td.Teacher == null)
        .ToList()
        .ForEach(td => YourDbContext.TimetableDetails.Remove(td));

    YourDbContext.SaveChanges();
}

I hope this helps.

For further reading, take a look at: Deleting orphans with Entity Framework.

Edit:

The example code above assumes that elsewhere in your code you have defined and created YourDbContext as a member variable within your repository class. I just wanted to point that out in case it wasn't clear.

Upvotes: 0

Related Questions