Reputation: 18511
I have two objects in my model
Car and carPart
with 1:n relationship.
I want to delete cascade the entity car. when I delete i get the following exception :
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 think it tries to delete the car object first and then the car parts.
Which is imposiible due to the foreign key.
How do I handle this ?
I want, obviously to delete the carPart first and then only the car.
Thanks.
Upvotes: 4
Views: 5432
Reputation: 7573
You'll need to tell the database that you want to cascade on delete, and then Entity Framework will do what you expect. You can change the FK behavior if you go to the Relationships
screen for a table in SQL Server Management Studio:
Upvotes: 6
Reputation: 13150
If you want Cascade delete then setup cascade delete at database level. You are getting error because SQL
is not allowing to deletion.
You don't have to do it in Entity Framework.
Upvotes: 1