HI WELCOME TO SIRIS

Data Annotations Attributes in EF 6 and EF Core

Leave a Comment

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

AttributeDescription
KeyCan be applied to a property to specify a key property in an entity and make the corresponding column a PrimaryKey column in the database.
TimestampCan be applied to a property to specify the data type of a corresponding column in the database as rowversion.
ConcurrencyCheckCan be applied to a property to specify that the corresponding column should be included in the optimistic concurrency check.
RequiredCan be applied to a property to specify that the corresponding column is a NotNull column in the database.
MinLengthCan be applied to a property to specify the minimum string length allowed in the corresponding column in the database.
MaxLengthCan be applied to a property to specify the maximum string length allowed in the corresponding column in the database.
StringLengthCan be applied to a property to specify the maximum string length allowed in the corresponding column in the database.

System.ComponentModel.DataAnnotations.Schema Attributes

AttributeDescription
TableCan be applied to an entity class to configure the corresponding table name and schema in the database.
ColumnCan be applied to a property to configure the corresponding column name, order and data type in the database.
IndexCan be applied to a property to configure that the corresponding column should have an Index in the database. (EF 6.1 onwards only)
ForeignKeyCan be applied to a property to mark it as a foreign key property.
NotMappedCan 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.
DatabaseGeneratedCan 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.
InversePropertyCan be applied to a property to specify the inverse of a navigation property that represents the other end of the same relationship.
ComplexTypeMarks 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.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.