Learn

There are more validation patterns that we can apply to the Country data model. This will change the display name for headers and <label> tags.

[Display(Name = "Code")]

The format of numeric and date data strings can be applied like this:

[DisplayFormat(DataFormatString = "{0:N0}", ApplyFormatInEditMode = true)]

Ranges with minimum and maximum values can be applied to numbers, dates, and strings:

[Range(1, 10000000000)]

Specific data types can be applied to fields:

[DataType(DataType.Date)]

Once the model is annotated, the database must be recreated to allow EF to modify the table schema to match the model design. This ensures that data is validated on the server using ModelState.IsValid and again when it is persisted to the database. We will see in the next exercise that validation can also happen in the browser.

Instructions

1.

Open the file Models/Country.cs. Add a using statement to support annotations:

  • System.ComponentModel.DataAnnotations
2.

Add [Required] to the first 3 properties.

3.

Force ID to be 2 characters uppercase letters only using StringLength and RegularExpression annotations. This matches what was added to the Continent model.

4.

Force ContinentID to be 2 characters uppercase letters only using StringLength and RegularExpression annotations.

5.

Make the display name more friendly by placing these above the corresponding field names:

  • Code instead of ID
  • UN Date instead of UnitedNationsDate
  • Continent instead of ContinentID
6.

Set a [Range] on Population so negatives are not allowed.

7.

Set [DisplayFormat] strings on Population and UN Date.

8.

Set a minimum date of 10/24/1945 on UN Date with a friendly message, using this format:

[Range(typeof(YOURTYPE), MIN, MAX, ErrorMessage = "MESSAGE")]
9.

Force DateTime? to use just the date — no time portion — by using [DataType(DataType.Date)].

Delete the Country.db file from the root of the project. Annotation changes to the models will need to be reflected in the database schema. Don’t worry, the sample data will reload on next use.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?