DataAnnotations (Data annotation) [Code-First series], dataannotations
EF Code-First provides a series of data annotation features that you can apply to your domain classes and attributes. The data annotation attributes overwrite the default EF conventions.
System. ComponentModel. DataAnnotationsIncludes attributes that impacts on nullability or size of the column. [This namespace contains the characteristics that affect the size and emptiness of data table columns .]
System. ComponentModel. DataAnnotations. SchemaNamespace includes attributes that impacts the schema of the database. [This namespace contains the characteristics that affect the data table]
Note: Data annotations only provide a subset of your configuration options. The complete configuration is in EF Fluent API.
System. ComponentModel. DataAnnotations Attributes:
| Attribute |
Description |
| Key |
Mark property as EntityKey which will be mapped to PK of the related table. [if the object attribute indicates the key feature, it will be the primary key of the data table] |
| Timestamp |
Mark the property as a non-nullable timestamp column in the database. [label column: cannot be blank] |
| ConcurrencyCheck |
ConcurrencyCheck annotation allows you to flag one or more properties to be used for concurrency checking in the database when a user edits or deletes an entity. [This attribute allows you to mark one or more attributes. It is used for concurrent checks when a user edits or deletes an object .] |
| Required |
The Required annotation will force EF (and MVC) to ensure that property has data in it. [The Field marked with this property cannot be blank. Make sure it has data] |
| MinLength |
MinLength annotation validates property whether it has minimum length of array or string. [used to verify whether the attribute has the smallest string or array length .] |
| MaxLength |
MaxLength annotation is the maximum length of property which in turn sets the maximum length of a column in the database [Set the maximum length for the field .] |
| StringLength |
Specifies the minimum and maximum length of characters that are allowed in a data field. [in the data field, specify the maximum and minimum character lengths] |
System. ComponentModel. DataAnnotations. Schema Attributes:
| Attribute |
Description |
| Table |
Specify name of the DB table which will be mapped with the class [Specify the name of the data table mapped to the class] |
| Column |
Specify column name and datatype which will be mapped with the property [specified column name] |
| Index |
Create an Index for specified column. (EF 6.1 onwards only) [Create an Index, which is used only in EF 6.1] |
| ForeignKey |
Specify Foreign key property for Navigation property [Specify a Foreign key for the Navigation property] |
| NotMapped |
Specify that property will not be mapped with database [Fields marked with this property will not be mapped to columns in the data table .] |
| DatabaseGenerated |
DatabaseGenerated attribute specifies that property will be mapped to computed column of the database table. so, the property will be read-only property. it can also be used to map the property to identity column (auto incremental column ). [The attribute marked with this feature will be mapped to the calculated column field of the data table, so this attribute will be read-only. It can also be used to map to auto-increment columns] |
| InverseProperty |
InverseProperty is useful when you have multiple relationships between two classes. [This property is useful when there are multiple relationships between two classes .] |
| ComplexType |
Mark the class as complex type in EF. [Mark this class as a complex type] |
In the following section, we will learn the attributes below in detail.