The DELETE statement conflicted with the REFERENCE constraint

libdez 21 Reputation points
2022-09-22T11:01:44.133+00:00

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.

Developer technologies | ASP.NET | ASP.NET Core
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 47,441 Reputation points
    2022-09-22T11:10:34.167+00:00

    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


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.