UniqueConstraintException: Parse out index name and duplicate values #70
Replies: 5 comments 1 reply
-
I have thought about it but I don't know if the constraint name is included in the message on every database. Also, the error message can be localized by the database so I would need to support all the languages supported by the database server for it to work properly. |
Beta Was this translation helpful? Give feedback.
-
Understood. I knew it was more complicated than I require for my own needs.
I wonder if the ConstraintName could simply be left at null if the database system doesn’t support it? Of course, the error message is likely a larger issue. I’m fortunate in that all my clients are English-speaking so I haven’t worried about multiple languages in my own code.
I guess for my use, I can create MyUniqueConstraintException, and then in my DbContext derivative, in very loose pseudocode, I can simply do this:
catch(UniqueConstraintException ex)
{
// Parse the index name and key values in English
throw new MyUniqueConstraintException(IndexName, KeyValues, ex);
}
That’s not difficult for me to do within my SQLServer and English-only environment, so I’ll just go that route.
Thank you for listening. You can feel free to close this discussion if you like 😊
From: Giorgi Dalakishvili ***@***.***>
Sent: Tuesday, February 27, 2024 10:24 AM
To: Giorgi/EntityFramework.Exceptions ***@***.***>
Cc: EricHarmon ***@***.***>; Author ***@***.***>
Subject: Re: [Giorgi/EntityFramework.Exceptions] UniqueConstraintException: Parse out index name and duplicate values (Discussion #70)
I have thought about it but I don't know if the constraint name is included in the message on every database. Also, the error message can be localized by the database so I would need to support all the languages supported by the database server for it to work properly.
—
Reply to this email directly, view it on GitHub<#70 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ADZX4NU5YL7GY56ISEBXEJ3YVX3CTAVCNFSM6AAAAABD4JUVC6VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DMMBWGY4DQ>.
You are receiving this because you authored the thread.Message ID: ***@***.******@***.***>>
|
Beta Was this translation helpful? Give feedback.
-
This is now implemented in the latest commit. Thanks to @NickStrupat for the idea |
Beta Was this translation helpful? Give feedback.
-
This is now included in the 8.1.0 version of the library. See ReadMe for more details. |
Beta Was this translation helpful? Give feedback.
-
This is great! It allowed me to simplify my code some. I'm doing a similar thing with ReferenceConstraintException, where I'm catching it, parsing out the reference name, the referenced table name, and the referenced column name, and then rethrowing the exception. I'd like to request an addition to the ReferenceConstraintException, where it will do the parsing for me as well. |
Beta Was this translation helpful? Give feedback.
-
I just started implementing this package today, to replace some home-grown code that I have. I sometimes handle unique constraint exceptions differently in my code; for example, a duplicate on IX_JobNumber might display a message such as "That job number is in use. You must supply a unique job number, or leave the job number blank". A duplicate on IX_SKU (for the same table) might display a message such as "Please enter a unique SKU".
It would be useful to me if this library would parse the error message appropriately for each supported database, and provide the index name as a property string ConstraintName, and perhaps the list of values as a property List ConstraintValues.
Beta Was this translation helpful? Give feedback.
All reactions