0
votes

I have a table mapped in the Entity Framework 4 that is the child table to a few other tables. Any time a try to delete a row in that table I get the message below. This table has multiple foreign keys to other tables but it is always the child table in the relationship, never the primary. This message happens as soon as I call context.DeleteObject(object), it doesn't need the context.SaveChanges() called. I have verified that all the relationships are defined correctly in the .edmx designer.

Message: 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.

Tables

Project (1)  -   (many) ProjectMember (many)  -  (1) Employee
   ProjectID (PK)            ProjectMemberID (PK)         EmployeeID (PK)
   ProjectName (varchar)     ProjectID (FK)               FirstName (varchar)
                             EmployeeID (FK)              LastName (varchar)
                             Role (varchar)

For every Project, I have multiple ProjectMembers which come from the list of Employees in the company. Deleting a ProjectMember should have no affect on either the Project or Employee table.

I don't want the FKs to be nullable because the relation must exist.

The deletion happens from a WPF 4 datagrid when delete is hit on the row. I intercept the delete key and call context.DeleteObject( ).

CommandManager.AddPreviewExecutedHandler(grid, new ExecutedRoutedEventHandler(gridDeleteCommandHandler));

private void gridDeleteCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
   if (e.Command == DataGrid.DeleteCommand)
   {
    if (grid.SelectedItem is ProjectMember)
    {
        ProjectMember pm = (ProjectMember)grid.SelectedItem;
        _context.DeleteObject(pm);
        SaveChanges();
    }
   }
    e.Handled = true;
}

Any ideas on why the error is occurring and how do I get the delete to work? I have no problem deleting from the primary table if there are no dependent rows in the child

1
Can you provide the relevant portions of your schema definition? - StriplingWarrior
I have updated the post with more info. If you need something else, let me know. - Doug

1 Answers

0
votes

Found the problem. To get to this data, I had two grids on the screen. The first listed all the Projects, the second would list the ProjectMembers for the selected Project. I would populate the ProjectMember grid using the same context that was used to populate the Project grid. Somehow, and I don't understand this part, the relationship that produced the error would be established and enforced. But, if I populate the ProjectMember grid using a different context, I am able to delete the ProjectMembers as you would expect. I do not know why the Entity Framework behaves this way but it works for me.