c# - Error message for Index data annotation in EF -
hi using entity framework 6.1.1 supports index info annotation feature in it. have field defined in entity class as:
[index("scoreindex", isunique=true)] public int score{ get; set; }
this working fine. however, trying figure out how display message when score not unique. right throws exception. tried next
[index("scoreindex", isunique=true, errormessage="score must unique")]
but not contain definition errormessage index annotation class. can please tell me how handle exception message handles gracefully?
the indexattribute
not validation attribute, , why doesn't have errormessage property, , doesn't have isvalid() method used validate against range of valid values.
that means not designed validated typical validation attributes (required, maxlength etc.). isunique attribute used during table creation create unique index.
if want utilize attributes, should create custom attribute check uniqueness of index. index of course of study inherit validationattribute
class, , have access ef dbcontext internally check uniqueness in attribute validation code.
if don't data-annotation approach, , it's complex, can handle dbupdateexception
thrown savechanges() method in try-catch block, decode error message , homecoming friendly in view-model.
try { using (var ac = new applicationdbcontext()) { // add together non-unique info ac.savechanges(); } } grab (dbupdateexception ex) { // handle index error }
c# .net entity-framework data-annotations
No comments:
Post a Comment