IndexAttribute not a validation attribute, and therefore it does not have the ErrorMessage property, nor does it have the IsValid () method, which is used to validate it against a range of valid values.
This means that it is not intended to be validated as typical validation attributes (Required, MaxLength, etc.). The IsUnique attribute is used only when creating a table to create a unique index.
If you want to use attributes, then you must create a custom attribute to verify that the index is unique. Of course, this index inherits the ValidationAttribute class and will have to internally access the EF DbContext to verify the uniqueness of the attribute verification code.
If you donβt like this approach to data annotation and it is too complicated, you can handle the DbUpdateException by the SaveChanges () method in the try-catch block, decode the error message and return something friendly in your look model.
try { using (var ac = new ApplicationDbContext()) {
source share