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