Q:

SQL Server FOREIGN KEY ON DELTE throw custom message

Is it possible to raise a custom message in SQL Server when trying to delete an entity with a foreign key violation? I use EntityFramework 6 and I would like to have a custom SqlException raised by sql server to better specify the error. Today EntityFramework raises the exception caused by sql server and I don't want to parse the general sql exception messages to specify such issues.
This is the exception:
    System.Exception: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The MERGE statement conflicted with the REFERENCE constraint "FK_My_ForeignKey"....

I think about something like this:
...
CONSTRAINT [FK_My_ForeignKey] FOREIGN KEY([Code]) REFERENCES [dbo].[MyTable] ([Code]) ON DELETE No Action AND RAISEERROR('MyCustomFKViolationError')
...

Or is there a better way to handle this via the `EntityFramework?

A:

Changing this behavior in SQL Server would be a little strange. You can overcome this problem simply in .Net/Entity Framework. Just look at this post to overcome it:
https://stackoverflow.com/a/2403361/1666800

