Data Annotations in Entity Framework Core

Data Annotations allow us the configure the model classes by adding metadata to the class and also the class properties. The Entity Framework Core (EF Core) recognizes these attributes and uses them to configure the models. We have seen how to use Entity Framework Core Convention to configure our models in our previous tutorials. The conventions have their limits in their functionalities. Data Annotations in Entity Framework Core allow us to further fine-tune the model. They override the conventions.

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 Entity Framework Core. They are part of a larger .NET  /.NET Core Framework. The ASP.NET MVC or ASP.NET MVC Core Applications also uses these attributes to validate the model.

The Data annotation attributes falls into two groups depending functionality provided by them.

  1. Data Modeling Attributes
  2. Validation Related Attributes

Data Modelling Attributes

Data Modeling Attributes specify the schema of the database. These attributes are present in the namespace  System.ComponentModel.DataAnnotations.Schema.The following is the list of attributes are present in the namespace.

AttributeDescription
Table AttributeYou can specify the name of the table to which the entity class maps to using table attributes
Column AttributeAllows us to specify the name of the column.
ComplexType AttributeThis attribute specifies that the class is a complex type.
DatabaseGenerated AttributeWe use this attribute to specify that the database automatically updates the value of this property.
ForeignKey AttributeWe apply Foreign Key Attribute to a property, which participates as a foreign key in a relationship
InverseProperty AttributeSpecifies 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 AttributeSpecifies the property, which you do not want to be in the database table. The Entity Framework will not create a column for this property.
IndexSpecify this attribute on a property to indicate that this property should have an index in the database

Validation Related Attributes

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

ATTRIBUTEDESCRIPTION
ConcurrencyCheck AttributeApply this attribute to a property, which participates in concurrency check validation while updating or deleting an entity
Key AttributeSpecify the properties, that are part of the primary key
MaxLength AttributeThis validation attribute specifies the max length of the column in the database
MinLength AttributeThis validation attribute specifies the minimum length of the data allowed in a string or array property.
Required AttributeSpecifies that a data field value is required. Specify the column as non-nullable
StringLength AttributeSpecifies the minimum and maximum length of characters that are allowed in a data field. This attribute is similar to MaxLength & MinLength attribute
Timestamp AttributeSpecifies the data type of the column as a row version.

Summary

Entity Framework Core 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.

References

  1. Conventions in Entity Framework Core
  2. System.ComponentModel.DataAnnotations.Schema
  3. System.ComponentModel.DataAnnotations

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top