question

libdez-4884 avatar image
0 Votes"
libdez-4884 asked libdez-4884 commented

The DELETE statement conflicted with the REFERENCE constraint

iam trying to delete an object from RacineDesReferntiel table that has a relational with service table how to fix this problm
its a aspnet web api project and i use the remove built in function and the same problm happen
please help me to fix this problm


fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (32ms) [Parameters=[@p0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30'] SET NOCOUNT ON;
DELETE FROM [racine des referentiels]
WHERE [id] = @p0;
SELECT @@ROWCOUNT;
fail: Microsoft.EntityFrameworkCore.Update[10000]
An exception occurred in the database while saving changes for context type 'Referentiels.Data.referentielsContext'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): The DELETE statement conflicted with the REFERENCE constraint "FK_services_racine des referentiels". The conflict occurred in database "referentiels", table "dbo.services", column 'racineid'.
The statement has been terminated.

sql-server-generaldotnet-aspnet-core-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered libdez-4884 commented

The DELETE statement conflicted with the REFERENCE constraint "FK_services_racine des referentiels"

As the message says, there is a foreign key reference on an other table, which holds referencing data.
You have to delete first the referencing data, before you can delete the reference data.

Other option is to define ON DELETE CASCADE on the FK, but that can be dangerous, if one delete the wrong reference data.
See https://learn.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?view=sql-server-ver16
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

i use the Include method to get the object and the object inside and then i delete all thats work for me thanks

0 Votes 0 ·