data validation
[Required] [MinLength(3,ErrorMessage = "name length at least 3")] [Display(Name = "name")] public string name {get; set;}
[Required] indicates Required,
[MinLength(3,ErrorMessage = "name length is at least 3")], which means that the name length is at least 3 characters
[Display(Name = "name")] name is displayed here as a name
[range (16100, ErrorMessage = "age range 16-100")] range validation
ErrorMsg is the prompt message displayed when the verification fails.
The data verification feature in EFCore mainly comes from system ComponentModel. Dataannotations namespace
The CustomValidationAttribute can be validated using user-defined validation methods.
However, it is common to customize validation attributes by inheriting ValidationAttribute.
As follows:
public class Student { public int Id { get; set; } [Required] [MinLength(3,ErrorMessage = "The name must be at least 3 long")] [Display(Name = "name")] public string Name { get; set; } [CheckSex] public string Sex { get; set; } [Range(16,100,ErrorMessage = "The age range is 16-100")] public int Age { get; set; } public ICollection<Course> Courses { get; set; } } [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class CheckSexAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { bool result= value.ToString() == "male" || value.ToString() == "female"; return result ? ValidationResult.Success : new ValidationResult("Gender must be male or female"); } }
In addition, you can also define some relevant validation rules through the RegularExpressionAttribute. Continue to take the gender above as an example for relevant validation.
Modify the student as follows:
public class Student { public int Id { get; set; } [Required] [MinLength(3,ErrorMessage = "The name must be at least 3 long")] [Display(Name = "name")] public string Name { get; set; } [RegularExpression("[0-1]",ErrorMessage = "Gender can only be the number 0(male)Or 1(female)")] public string Sex { get; set; } [Range(16,100,ErrorMessage = "The age range is 16-100")] public int Age { get; set; } public ICollection<Course> Courses { get; set; } }
Common verification methods are as follows:
RequiredAttribute: used to validate the required data fields.
RangeAttribute: used to verify whether the value of the numeric field is within the specified range
StringLengthAttribute: used to verify whether the string length of the target field is within the specified range.
MaxLengthAttribute/MinLengthAttribute: used to verify whether the length of the character / array dictionary is less than / greater than the specified upper / lower limit.
CreditCardAttribute is used to verify that the data field value is the credit card number.
EmailAddressAttribute is used to validate e-mail messages
PhoneAttribute is used to verify whether it is in phone format
UrlAttribute is used to verify whether it is Url
KeyAttribute validation is unique
DisplayAttribute specifies the string to display
CompareAttribute provides attributes for comparing two attributes.
Special note:
When building an entity class, if the Key value is not specified, EFCore will default to
Id, the attribute of class name + Id is used as the primary key of the data table.
3. Conflict between database schema and entity class inconsistency
3.1 entity attributes do not need to be stored in the database
Add a new attribute nickname (nickname) to the student class, but this attribute does not need to be stored in the database, so just add a NotMapped feature to the nickname and it will not correspond to the data table.