|
-
Dec 20th, 2015, 09:49 PM
#1
Thread Starter
PowerPoster
MVC, Model, Index DataAnnotation issue.
Hi, guys,
I have a problem that I have not been able to figure out.
In some of my MSSQL columns, I am requiring that the information inserted is unique. Such as ITIN or SSN, FEIN etc.
The values are string, so they are automatically nullable based on my research.
I am using the [Index] DataAnnotation on these columns to ensure that the values inserted are unique.
However, my problem is that because I have the [Index] DataAnnotation in use on the model. If I register an account in the MVC Web App for the first time and don't specify a value for ITIN, SSN or FEIN, the first registration succeeds. If I register subsequent accounts, I get a run-time error advising can't insert a duplicate entry for NULL.
What I would like to do, if possible, is only allow unique values if something is inputted by the end user except for NULLS. Any suggestions would be appreciated.
Code:
[Column("ITIN")]
[DataType(DataType.Text)]
[Display(Name = "ITIN")]
[MinLength(0), MaxLength(11), StringLength(11, ErrorMessage = "The {0} must be no more than {2} characters in length and in the format of ###-##-####. This is optional.", MinimumLength = 0)] // XXX-XX-XXXX
[Index("UQ_UserInfo_ITIN", IsClustered = true, IsUnique = true)]
[DisplayFormat(ConvertEmptyStringToNull = true)]
[Required(AllowEmptyStrings = true,
ErrorMessage = "The {0} is required. Please enter in a valid {0}."),
ConcurrencyCheck]
public string ITIN { get; set; }
Last edited by BrailleSchool; Dec 20th, 2015 at 10:50 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|