December 23, 2017 at 1:04 pm
I have a table with a constraint on 2 columns (Which are keys into 2 other tables) and I want to prevent duplications
I am using Entity Framework and message sent back to application if violation is encountered
[1234,5678 ] is duplicated (i.e. It sends back the 2 keys that would be duplicated if I saved)
(e.g. I am adding a Country and a City to a third table, and want to prevent duplications where the same Country and City are attempted to be added)
e.g.
Country Table City Table
001 Ireland 100 Dublin
002 France 200 New York
003 USA 300 Paris
So if I try to add
Ireland Dublin
Ireland Dublin I get back [001,100] duplication error (I think this is Error Code 40005)
I'd prefer to get back
[Ireland, Dublin] Dupliction error attempted
I would prefer to send back, not the Primary Keys, which are meaningless, but two other columns of descriptions
Is this possible ?
December 24, 2017 at 6:56 am
If you're using Entity Framework, and it's running ad-hoc SQL against the DB, no. Handle the error in your application and present to the user something meaningful.
If you have a stored procedure, use TRY... CATCH and RAISERROR
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 25, 2017 at 4:39 am
GilaMonster - Sunday, December 24, 2017 6:56 AMIf you're using Entity Framework, and it's running ad-hoc SQL against the DB, no. Handle the error in your application and present to the user something meaningful.
If you have a stored procedure, use TRY... CATCH and RAISERROR
Many thanks for your reply
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply