Data Annotations in Entity Framework are used to define constraints, validations, and other data-related attributes directly in your C# code. They are used to configure the database schema, data types, and validation rules for your entities. Here’s a list of common Data Annotations in Entity Framework along with an example code:

 

In Data Annotations, we have two types of namespaces that have their specific in-build types.

1. System.ComponentModel.DataAnnotations incorporate the subsequent attributes that affect and check the size or nullability of the column.

2. System.ComponentModel.DataAnnotations.Schema namespace comprises the subsequent attributes that reshape the schema of the database.

 

Why and When to Use Data Annotations

Data annotations (DAs) are used to provide metadata to your code, which can help to control the behavior of your code. By using DAs in your code, you can ensure that your application is more robust, reliable, and maintainable. Here are some reasons why and when you might use data annotations:

  1. Data Validation — DAs can be used to enforce data validation rules, ensuring that data entered into your application meets certain requirements. For example, the [Required] annotation can be used to ensure that a property is not null or empty, and the [Range] annotation can be used to ensure that a numeric property falls within a specific range.
  2. Display Formatting — DAs can be used to control how data is displayed in your application’s user interface. For example, the [DisplayFormat] annotation can be used to control the formatting of a date or time property.
  3. Data Mapping — DAs can be used to specify how your entity is mapped to the database. For example, the [Key] annotation can be used to specify the primary key for your entity, and the [ForeignKey] annotation can be used to specify the foreign key for a navigation property.
  4. Model Binding — DAs can be used to control how data is bound to your application’s model. For example, the [Bind] annotation can be used to specify which properties should be included or excluded when binding to a model.
  5. Concurrency Control — DAs can be used to handle concurrency issues when updating data. For example, the [Timestamp] annotation can be used to include a timestamp property in your entity, which can be used to detect when data has been modified by another user.
System.ComponentModel.DataAnnotations Related Attributes

Here, is a list of some important Data Annotation Attributes.

1. Required: Specifies that the Input field cannot be empty. Example:

2. DisplayName: Specifies the Display Name for a Property. Example:

3. StringLength: Specifies minimum and maximum length for a property. Example:

4. Range: Specifies a range of numeric values. Example:

5. Bind: Include or Exclude value when adding form values to model properties. Example:

6. ScaffoldColumn: Specifies a field for hiding from editor forms. Example:

7. DisplayFormat: Specifies a display format for a property like Date Format, Currency format etc. Example:

8. ReadOnly: It set a property to read-only. Example:

9. MaxLength: Specifies max length of string. Example:

10. CreditCard: specifies that a data field value is credit card number. Example:

11. Compare: compare with other input fields. Example:

12. EmailAddress: specifies that an input field value is a well-formed Email Address using Regular Expression. Example:

13. Phone: specifies that an input field value is well-formed phone number using Regular Expression. Example:

Output format: 91-1234-567-890

14. Url: specifies URL validation. Example:

15. RegularExpression: specifies that the input field is matched with the desired Regular Expression. Example:

16. DataType: provides a list of data type that is associated with input field and parameter. Example:

17. HiddenInput: specifies a hidden input field. Example:

System.ComponentModel.DataAnnotations.Schema Related Attributes

1. Table: Specifies the name of the database table associated with an entity class.

2. Column:  Specifies the name and data type of a database column associated with a property.

3. Index:  Specifies an index on one or more columns of a table.

4. ForeignKey:  Specifies a foreign key relationship between entities.

5. NotMapped:  Excludes a property or class from being mapped to the database.

6. DatabaseGenerated: Defines how a value for a database column is generated, such as identity, computed, or none.

7. InverseProperty:  Specifies the inverse navigation property in a many-to-many relationship.

8. ComplexType: Marks a class as a complex type, which is a class that doesn’t have a primary key and is used as part of another entity.

9. DefaultValue: DefaultValue data annotation is used to specify a default value for a property.

 

These are some of the commonly used Data Annotations in Entity Framework. You can use these attributes to define the structure, constraints, and relationships of your data model when working with Entity Framework.

Leave a Comment