6

I have a problem with the removal of items from the database if connects them a many to many relationship

My database look like

| [Project] | <-- | [JobInProject] | --> | [Job] |
=============     ==================     =========
| ProjectID |     | JobInProjectID |     | JobID |
|           |     | ProjectID      |     |       |
|           |     | JobID          |     |       |

Primary keys from Project and Job table are also set as foreign key in others tables but I think it isn't problem because when I remove item from Job table, it is removed correctly with all related items in others tables

All foreign keys are set by constraint and on delete cascade on update cascade

Code which I use to delete job item

 Job job = await db.Jobs.FindAsync(id);
 db.Entry(job).State = EntityState.Deleted;
 await db.SaveChangesAsync();    

and project:

Project project = await db.Projects.FindAsync(id);
db.Entry(project).State = EntityState.Deleted;
await db.SaveChangesAsync();

Code to remove project item remove only data from Project table and JobInProject table do not remove Job and related items.

When I modify code to remove Project like this:

 Project project = await db.Projects.FindAsync(id);

 foreach (var item in project.JobInProjects)
 {
     db.Entry(item.Job).State = EntityState.Deleted;
 }

 db.Entry(project).State = EntityState.Deleted;
 await db.SaveChangesAsync();

I get an error on the await db.SaveChangesAsync(); line

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.

How can I remove Project item and related Job items ?

I will be grateful for help

1
  • Primary keys from Project and Job table are also set as foreign key in others tables, how can you have a single foreign key to the other entity in a many-to-many relation? This implies you have one-to-one Commented Mar 25, 2016 at 10:57

1 Answer 1

2

It's because you don't mark the JobInProject object as Deleted:

foreach (var item in project.JobInProjects)
{
    db.Entry(item.Job).State = EntityState.Deleted;
    db.Entry(item).State = EntityState.Deleted; // <= line added
}

If you don't do that, EF will assume you don't want to delete the item and try to set both foreign keys in JobInProject to null, but of course one or more of the foreign-key properties is non-nullable (both aren't).

Sign up to request clarification or add additional context in comments.

Comments

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.