Data Annotations Attributes in EF 6 and EF Core:

Data Annotations attributes are .NET attributes which can be applied on an entity class or properties to override default conventions in EF 6 and EF Core.

Data annotation attributes are included in the System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema namespaces in EF 6 as well as in EF Core. These attributes are not only used in Entity Framework but they can also be used with ASP.NET MVC or data controls.

These data annotation attributes work in the same way in EF 6 and EF Core and are valid in both.

Note: Data annotations only give you a subset of configuration options. Fluent API provides a full set of configuration options available in Code-First.

System.ComponentModel.DataAnnotations Attributes:

Attribute Description
Key Can be applied to a property to specify a key property in an entity and make the corresponding column a PrimaryKey column in the database.
Timestamp Can be applied to a property to specify the data type of a corresponding column in the database as rowversion.
ConcurrencyCheck Can be applied to a property to specify that the corresponding column should be included in the optimistic concurrency check.
Required Can be applied to a property to specify that the corresponding column is a NotNull column in the database.
MinLength Can be applied to a property to specify the minimum string length allowed in the corresponding column in the database.
MaxLength Can be applied to a property to specify the maximum string length allowed in the corresponding column in the database.
StringLength Can be applied to a property to specify the maximum string length allowed in the corresponding column in the database.

System.ComponentModel.DataAnnotations.Schema Attributes:

Attribute Description
Table Can be applied to an entity class to configure the corresponding table name and schema in the database.
Column Can be applied to a property to configure the corresponding column name, order and data type in the database.
Index Can be applied to a property to configure that the corresponding column should have an Index in the database. (EF 6.1 onwards only)
ForeignKey Can be applied to a property to mark it as a foreign key property.
NotMapped Can be applied to a property or entity class which should be excluded from the model and should not generate a corresponding column or table in the database.
DatabaseGenerated Can be applied to a property to configure how the underlying database should generate the value for the corresponding column e.g. identity, computed or none.
InverseProperty Can be applied to a property to specify the inverse of a navigation property that represents the other end of the same relationship.
ComplexType Marks the class as complex type in EF 6. EF Core 2.0 does not support this attribute.

Learn about each data annotation attribute in the subsequent chapters.