Исключение индекса таблицы в C#C#

Место общения программистов C#
Ответить
Anonymous
 Исключение индекса таблицы в C#

Сообщение Anonymous »


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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»