In this tutorial, we will give you a short introduction to the data annotations in Entity Framework & Entity Framework Core (EF Core). The Data annotations attribute classes are used to decorate the classes or properties. These attributes help Entity Framework Code First & EF Core to apply pre-defined rules on entity models
We have seen how to use Entity Framework code first conventions or Entity Framework Core Convention to configure our models in our previous tutorials. The conventions are good but are limited in their functionalities. .EF uses the Data Annotations to further
In this article
What is Data Annotation Attributes
The Data Annotations are the attributes that are applied to the class or on the properties of
The Data annotation attributes are grouped into two categories depending functionality provided by them
- Data modelling attributes
- Data Validation Attributes
Data modelling attributes
The Data modelling attributes specify the schema of the database. These attributes are present in the namespace System.ComponentModel.DataAnnotations.Schema.
The following attributes are present in the above namespace
Attribute | Description |
---|---|
Table Attribute | You can specify the name of the table to which the entity class maps to using these attributes |
Column Attribute | Allows us to specify the name of the column. |
ComplexType Attribute | This attribute specifies that the class is a complex type. |
DatabaseGenerated Attribute | This attribute added on the properties whose value is automatically updated by the Database. |
ForeignKey Attribute | Foreign Key Attribute is applied to a property, which participates as a foreign key in a relationship |
InverseProperty Attribute | Inverse Property is used when you have many to many relationships between entities. Specified on the property which is at the other end of the relationship |
NotMapped Attribute | The NotMapped attribute is applied to those properties, which you do not want to include in your database table. |
Index | This attribute placed on a property to indicate that this property should have a index in the database |
The Validation related attributes reside in the System.ComponentModel.DataAnnotations namespace. These attributes are used to enforce validation rules for the entity properties.
ATTRIBUTE | DESCRIPTION |
---|---|
ConcurrencyCheck Attribute | This attribute is applied to a property, which participates in concurrency check validation while updating or deleting an entity |
Key Attribute | This attribute is applied to the property or properties, that are part of the primary key |
MaxLength Attribute | This validation attribute specifies the max length of the column in the database |
MinLength Attribute | This validation attribute specifies the minimum length of the data allowed in a string or array property. |
Required Attribute | Specifies that a data field value is required. Specify the column as non-nullable |
StringLength Attribute | Specifies the minimum and maximum length of characters that are allowed in a data field. This attribute is similar to MaxLength & MinLength attribute |
Timestamp Attribute | Specifies the data type of the column as a row version. |
Conclusion
Entity Framework allows us various ways to configure the model class. Using Conventions, using data annotation attributes or by using fluent API. In this tutorial, we looked at Data annotation attributes. In the next few tutorials, we will look at each of the above data annotations attributes and how to use them with examples.
Leave a Reply