Back to: ASP.NET Core Tutorials For Beginners and Professionals
Data Annotation Attributes in Entity Framework Core
In this article, I am going to discuss How to Configure Domain Classes with Data Annotation Attributes in Entity Framework Core with Examples. Please read our previous article, where we discussed 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, which are followed by Entity Framework Core.
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
The following Data Annotation Attributes impact the Schema of a database. 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 present in the namespace.
- 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 are used to specify the corresponding database table name and schema.
- 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.
- 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.
- 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.
- NotMapped: The NotMapped Attribute can be applied to a property or entity class that should be excluded from the model and should not generate a corresponding column or table in the database. That means it denotes that a property or class should be excluded from database mapping.
- InverseProperty: The InverseProperty Attribute can be applied to a property to specify the inverse of a navigation property that represents the other end of the same relationship.
- ComplexType: This attribute specifies that the class is a complex type.
- 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 System.ComponentModel.DataAnnotations namespace.
- 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.
- 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.
- 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.
- 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.
- MinLength: The MinLength Attribute can be applied to a property of a domain entity to specify the minimum string length allowed in the corresponding database column. That means it specifies the minimum length of array or string data allowed in a property.
- 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 array or string data allowed in a property.
- 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 corresponding database column. That means it specifies the minimum and maximum length of characters that are allowed in a data field.
From the next article onwards, we are going to discuss all the above Attributes with examples.
In the next article, I am going to 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.