Data Annotations allow us the configure the model classes by adding metadata to the class and also the class properties. The Entity Framework recognizes these attributes and uses them to configure the models. We have seen how to use Entity Framework Code first conventions to configure our models in our previous tutorials. The conventions have their limits in their functionalities. .Data Annotations in Entity Framework allow us to further
Table of Content
What is Data Annotation
Data Annotations are the attributes that we apply to the class or on the properties of the class. They provide additional metadata about the class or its properties. These attributes are not specific to the Entity Framework. They are part of a larger .NET /.NET Core Framework. The ASP.NET MVC Applications also uses these attributes to validate the model.
The Data annotation attributes falls into two groups depending functionality provided by them.
- Data Modeling Attributes
- Validation Related Attributes
Data Modelling Attributes
Data Modeling Attributes specify the
schema of the database. These attributes are present in the namespace
a.The following is the list of attributes are present in the namespace.
|Table Attribute||You can specify the name of the table to which the entity class maps to using table 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||We use this attribute to specify that the database automatically updates the value of this property.|
|ForeignKey Attribute||We apply Foreign Key Attribute to a property, which participates as a foreign key in a relationship|
|InverseProperty Attribute||Specifies the inverse of a navigation property. We use this when we have multiple relationships between two entities. Apply it on a property which is at the other end of the relationship|
|NotMapped Attribute||Specifies the property, which you do not want to be in the database table. The Entity Framework will not create a column for this property.|
|Index||Specify this attribute on a property to indicate that this property should have an index in the database|
Validation related Attributes reside in the
System.ComponentModel.DataAnnotations namespace. We use these attributes to enforce validation rules for the entity properties. These attributes also define the size, nullability & Primary key, etc of the
|ConcurrencyCheck Attribute||Apply this attribute to a property, which participates in concurrency check validation while updating or deleting an entity|
|Key Attribute||Specify the 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.|
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.