Entity Framework Data Annotations Discover How to use Attributes
In .NET Framework, data annotation add extra meaning to the data by adding attribute tags. It is used to configure the classes which will highlight the most commonly needed configurations.
- Data Annotations attributes are .NET attributes which can be applied to an entity class or properties to override default CodeFirst conventions in EF6 and EF Core.
- The advantage of using Data Annotation feature is that by applying Data Attributes, we can manage the data definition in a single place and do not need re-write the same rules in multiple places.
- It can be used in a number of .NET applications, such as ASP.NET MVC which allows these applications to leverage the same annotations for client-side validations.
The mostly used Data Annotations attributes are as follows;
Attribute | Description |
---|---|
Key | To make the corresponding column a primary pey (PK) column in the database. |
ForeignKey | Specifies that the property is the foreign key in a relationship. |
Required | To make the corresponding column a NOT NULL column in a database table. |
MinLength | Specify a minimum number of characters or bytes for the column that the property should map to. |
MaxLength | Specify a maximum number of characters or bytes for the column that the property should map to. |
StringLength | Sets the maximum allowed length of the property value. |
Table | Create a table with a specified name in Table attribute for a given domain class. |
Column | Create a column with a specified name in Column attribute for a given property in a domain class. |
ConcurrencyCheck | Specifies that the property is included in concurrency checks. |
Timestamp | Specifies the data type of the database column as rowversion. |
NotMapped | Applied to properties or classes that are to be excluded from database mapping. |
InverseProperty | Specifies the inverse of a navigation property. |
ComplexType | Complex Types cannot be tracked on their own but they are tracked as part of an entity. |
DatabaseGenerated | Specifies how the database generates values for the property. |
ZZZ Projects