Click here to Skip to main content
15,917,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two tables with a many to many relationship in Entity Framework 5 model, (MyLineItems and ItemDetails), Entity framework generated table MyLineItemsItemDetails

When i attempt to delete MyLineItems for a contract, I get a foreign key error. If I attempt to remove the items programatically, it removes the items from the MyLineItemItemDetails table but throws:

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.

I don't want the ItemDetails deleted, just the items in MyLineItemItemDetails removed so I can delete MyLineItems.

How can this be done. I've checked on line and have yet to find a working solution.

Code snippet: var items = from c in MyLineItems select c;
foreach (var b in items)
{
b.ItemDetails.Clear();
//or both get the same error

//var ce = (from d in b.ItemDetails select d).ToList();
//for(int i=0;i<ce.count();> //{

// b.ItemDetails.Remove(ce[i]);
//}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900