Исключение индекса таблицы в C# ⇐ C#
-
Anonymous
Исключение индекса таблицы в C#
I have a table called Announcement and it has unique index on columns Title and Code.
I am using Entity Framework 6.0.26 for CRUD operations, but I have an issue in inserting/updating data into the mentioned columns.
catch (Exception ex) { if (ex.InnerException!.Message.Contains("unique index")) { if (ex.InnerException.Message.Contains("IX_Announcement_Code")) { throw new Exception("this code is already used"); } if (ex.InnerException.Message.Contains("IX_Announcement_Title")) { throw new Exception("this title is already used"); } } throw new Exception(); } I am using this code to figure out what threw the exception - because of index or not, but I know it is a bad practice to do this.
Is there any exception related to table index?
I know that I can create my custom method for validating (e.g. Task TitleAlreadyExists(string title)) but this is not what I am looking for.
I am looking for a better solution for handling database table index exception.
Источник: https://stackoverflow.com/questions/781 ... in-c-sharp
I have a table called Announcement and it has unique index on columns Title and Code.
I am using Entity Framework 6.0.26 for CRUD operations, but I have an issue in inserting/updating data into the mentioned columns.
catch (Exception ex) { if (ex.InnerException!.Message.Contains("unique index")) { if (ex.InnerException.Message.Contains("IX_Announcement_Code")) { throw new Exception("this code is already used"); } if (ex.InnerException.Message.Contains("IX_Announcement_Title")) { throw new Exception("this title is already used"); } } throw new Exception(); } I am using this code to figure out what threw the exception - because of index or not, but I know it is a bad practice to do this.
Is there any exception related to table index?
I know that I can create my custom method for validating (e.g. Task TitleAlreadyExists(string title)) but this is not what I am looking for.
I am looking for a better solution for handling database table index exception.
Источник: https://stackoverflow.com/questions/781 ... in-c-sharp
Мобильная версия