In this article, I will be explaining how to use some properties of data annotation in order to make it easier to model your database and also to save your time with front-end validations.
How to create your database?
What is Data Annotation?
"Data Annotation provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls."
Why would I use Data Annotation?
There are 3 main areas where you may use it; two of them are related to your data presentation to your end user and one is to design your database.
- Front-End: Validation Attributes
Used to enforce validation rules in your view. It can be used to validate email, data, fields with masks, etc.
- Front-End: Display Attributes
Used to specify how your properties from your Model will be displayed. It can be used within Resources to display a different value depending on the user language.
- Back-End: Modelling Attributes
Used to specify the table limitations and the relationship between classes. It can be used to set field type, length, mask, etc.
How to do it?
Let's see it now. In this example, we are going to use a very simple trip manager model, TripManager class.
public class TripManager
{
[Key]
public int Id { get; set; }
[ForeignKey( "HotelID" )]
public Hotel Hotel { get; set; }
public int HotelID { get; set; }
[ForeignKey( "CarID" )]
public Car Car { get; set; }
public int CarID { get; set; }
[DataType( DataType.Date )]
public DateTime CheckInDate { get; set; }
[DataType( DataType.Date )]
public DateTime CheckOutDate { get; set; }
[Column( "TodaysPrice" )]
[Range( 10.30, 46.60 )]
public double Price { get; set; }
public Person Responsable { get; set; }
}
Hotel Class
public class Hotel
{
public int Id { get; set; }
[DisplayFormat( NullDisplayText = "Null name" )]
public string Name { get; set; }
}
Person Class
public class Person
{
public int Id { get; set; }
[StringLength( 50, ErrorMessage = "Name cannot be longer than 50 characters." )]
public string Name { get; set; }
[NotMapped]
public List<Place> VisitedPlaces { get; set; }
}
Car Class
public class Car
{
public int Id { get; set; }
[Required]
public string Model { get; set; }
public string Brand { get; set; }
}
Place Class
public class Place
{
public int Id { get; set; }
public string Location { get; set; }
}
Now, let's understand the application of each one data annotation attribute used here.
Key
[Key]
public int Id { get; set; }
Explanation
Sets the key of the table.
Result
Foreign Key
[ForeignKey( "HotelID" )]
public Hotel Hotel { get; set; }
public int HotelID { get; set; }
Explanation
Sets the foreign key of this relationship as the property described.
Result in the database
Foreign Key
[ForeignKey( "CarID" )]
public Car Car { get; set; }
public int CarID { get; set; }
Explanation
Same as before. Sets the foreign key of this relationship as the property described.
Result in the database
Data type
[DataType( DataType.Date )]
public DateTime CheckInDate { get; set; }
Explanation
Creates a UI mask based on your provided type. It's very useful and commonly seen used with data and password types. Find more types here.
Result
Column and Range
[Column( "TodaysPrice" )]
[Range( 10.30, 46.60 )]
public double Price { get; set; }
Column Explanation
Used to define the name of the column in the database of its respective table for the specified property.
Column Result in the database
Range Explanation
Used to validate the input on the client side according to the specified range of value.
Range Result
Required
[Required]
public string Model { get; set; }
Explanation
Specifies a property as required and does not accept null at the database.
Result in the database
String length
[StringLength( 50, ErrorMessage = "Name cannot be longer than 50 characters." )]
public string Name { get; set; }
Explanation
Sets the max length of its field in the database and validates the input in the UI.
Result in the database.
Result in the UI.
Not mapped
[NotMapped]
public List<Place> VisitedPlaces { get; set; }
Explanation
The property is ignored by the database.
Result in the database
Display format
[DisplayFormat( NullDisplayText = "Null name" )]
public string Name { get; set; }
Explanation
Configures how this field is going to be displayed to the end user. It's used with language resources and you can find more options here.
Result
Congratulations! You have successfully implemented Data Annotation with your ASP.NET Core application.