Reputation: 18521
I have the following class
public class ObjectA{
private List<ObjectB> list;
}
ObjectA
and ObjectB
are in N:N relation.
I want to delete only the relation and I use
while (objectA.list.Any())
objectA.list.Remove(objectA.list.First());
List is of the relation table -
List<ObjectAobjectB>
And I get
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.
EDIT: updating model definition
There are three tables in my model :
* ClassA - SchemaA,
* ClassAClassB - SchemaA,
* ClassB - SchemaB,
In my contex (and edmx ) I have only Schema A ( ClassA and ClassAClassB)
There for it is 1:N to the relation Table.
Here is the code generated from the edmx.
public partial class ClassA:DomainEntity
{
....
public virtual ICollection<ClassB> ClassAClassB { get; set; }
}
What am I doing wrong?
Thanks.
Upvotes: 1
Views: 451
Reputation: 364249
If you have one-to-many relation with non-nullable FK you must also delete ObjectB because removing it from navigation property will only remove the relation (makes FK null) but does not remove the ObjectB itself. Try this:
while (objectA.list.Any()) {
var b = b;
objectA.list.Remove(b);
entities.DeleteObject(b);
}
Upvotes: 1