Data Annotation Attributes in Entity Framework Core

Data Annotation Attributes in Entity Framework Core

In this article, I will discuss How to Configure Domain Classes with Data Annotation Attributes in Entity Framework Core with Examples. Please read our previous article discussing Default Conventions in Entity Framework Core with Examples.

Data Annotation Attributes in Entity Framework Core

Data Annotations are nothing but Attributes or Classes that can be applied to our domain classes and their properties to override the default conventions that Entity Framework Core follows.

The Data Annotations Attributes are included in separate namespaces called System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema.

Note: Data Annotation Attributes give you only a subset of configuration options in Entity Framework Core. You need to use Fluent API for the full configuration options.

Data Modelling Attributes in EF Core

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

  1. Table: The Table Attribute can be applied to a domain entity to configure the database’s corresponding table name and schema. That means it specifies the database table that a class is mapped to. It has two properties, i.e., Name and Schema, which specify the corresponding database table name and schema.
  2. Column: The Column Attribute can be applied to a property of a domain entity to configure the corresponding database column name, order, and data type. That means it represents the database column that a property is mapped to. It has three properties, i.e., Name, Order, and TypeName, to specify the database’s column name, order, and data type.
  3. Index: The Index Attribute can be applied to a property of a domain entity to configure that the corresponding database column should have an Index in the database.
  4. ForeignKey: The ForeignKey Attribute can be applied to a property of a domain entity to mark it as a foreign key column in the database. That means it denotes a property used as a foreign key in a relationship.
  5. NotMapped: The NotMapped Attribute can be applied to a property or entity class that should be excluded from the model and not generate a corresponding column or table in the database. That means that a property or class should be excluded from database mapping.
  6. InverseProperty: The InverseProperty Attribute is applied to a property to specify the inverse of a navigation property that represents the other end of the same relationship.
  7. ComplexType: This attribute specifies that the class is a complex type.
  8. DatabaseGenerated: We use this attribute to specify that the database automatically updates the value of this property.
Validation Related Attributes

The following Data Annotation Attributes impact the nullability or size of the column in a database, and these Attributes belong to the System.ComponentModel.DataAnnotations namespace.

  1. Key: The Key Attribute can be applied to a property of a domain entity to specify a key property and make the corresponding database column a Primary Key column. That means it denotes one or more properties that uniquely identify an entity. Specifies that a property is the primary key for an entity.
  2. Timestamp: The Timestamp Attribute can be applied to a property of a domain entity to specify the data type of the corresponding database column as a row version.
  3. ConcurrencyCheck: The ConcurrencyCheck Attribute can be applied to a property of a domain entity to specify that the corresponding column should be included in the optimistic concurrency check.
  4. Required: The Required Attribute can be applied to a property of a domain entity to specify that the corresponding database column is a NotNull column. That means it specifies that a data field value is required. Indicates that a property is required, resulting in a NOT NULL column in the database.
  5. MinLength: The MinLength Attribute can be applied to a property of a domain entity to specify the minimum string length allowed in the property. That means it specifies the minimum length of string data allowed in a property. It will not affect the database column. Specifies the minimum length of string data allowed in a property.
  6. MaxLength: The MaxLength Attribute can be applied to a property of a domain entity to specify the maximum string length allowed in the corresponding database column. That means it specifies the maximum length of string data allowed in a property. It will set the length of the column in the database. Specifies the maximum length of string data allowed in a property.
  7. StringLength: The StringLength Attribute can be applied to a property of a domain entity to specify the minimum and maximum string length allowed in the property. That means it specifies the minimum and maximum length of characters allowed in a data field.

Note: In Entity Framework Core (EF Core), there are two ways to configure domain classes and database design: Data annotations and the Fluent API. Data annotations are attributes placed on entity class properties. They provide EF Core with metadata information regarding the constraints, mappings, and other configurations that should apply to a database column linked with that property.

From the next article onwards, we will discuss all the above Attributes with examples. In the next article, I will discuss the Table Data Annotation Attribute in EF Core with Examples. Here, in this article, I try to explain What are Data Annotation Attributes in Entity Framework Core.

1 thought on “Data Annotation Attributes in Entity Framework Core”

Leave a Reply

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