Update after requested information:
As per our inner exception details its quite clear that there is a foreign key constraint conflict.
What that means is, before deleting a key that is referred in other table as foreign key, you have to delete the dependent rows first. You can also remove the constraint causing error to keep the rows dependent on key.
More information about how to achieve it using EF is here
In case the number of items to be deleted is not significantly large, you can use what Andy Nichols suggested.
But when you want to delete hundreds and thousands of items, you would notice that method is not performant. I recently encounter one such case and changed my method to what's shown below:
public void DeleteAll<T>(IEnumerable<T> items) where T : class
{
Guard.ArgumentNotNull(items, "items");
var autoDetectChangesEnabledBefore = Configuration.AutoDetectChangesEnabled;
var validateOnSaveEnabled = Configuration.ValidateOnSaveEnabled;
// It is said to make the performance better.
Configuration.AutoDetectChangesEnabled = false;
Configuration.ValidateOnSaveEnabled = false;
foreach (var item in items)
{
Set<T>().Attach(item);
Set<T>().Remove(item);
}
SaveChanges();
Configuration.AutoDetectChangesEnabled = autoDetectChangesEnabledBefore;
Configuration.ValidateOnSaveEnabled = validateOnSaveEnabled;
}
You can read about these flags here.
Turning both of these flags off has advantage and disadvantage. In my case it was serving more good (huge performance saving) then bad. Open for comments on if there will be any side effects (haven't seen any in last one month).