[Table] → Maps an entity to a specific table (optionally with schema).
[Index] (EF Core 5+) → Defines database indexes on properties.
[Owned] → Marks a type as an owned entity.
[Keyless] → Marks an entity as having no primary key (useful for views).
[Key] → Marks property as primary key.
[Required] → Makes property non-nullable.
[MaxLength] / [MinLength] / [StringLength] → Controls string/array length.
[Column] → Configures column name, type, and order.
[DatabaseGenerated] → Specifies value generation strategy (Identity, Computed, None).
[ConcurrencyCheck] → Marks property for optimistic concurrency.
[Timestamp] → Special concurrency token (rowversion).
[NotMapped] → Excludes property from database mapping.
[Precision] (EF Core 6+) → Sets precision and scale for decimals.
[ForeignKey] → Marks property as foreign key.
[InverseProperty] → Specifies the inverse navigation property.
[Required] (on navigation) → Ensures relationship is not optional.
[Range] → Numeric/date range validation.
[RegularExpression] → Regex validation.
[EmailAddress], [Phone], [Url], [CreditCard] → Specialized format validation.
[Compare] → Compare two properties (e.g., password confirmation).
[Display], [DisplayFormat], [DataType] → UI metadata and formatting.
[Editable], [ScaffoldColumn] → UI scaffolding hints.
0. [ValidateNever]
When you have a property that should be bound but not validated.
Example: A navigation property in EF Core that you don’t want MVC to validate.
1. `[Required]`
The [Required] annotation ensures that a property cannot be null.
public class Person
{
          [Required]
          public string FirstName { get; set; }
}
Â
Â
2. `[StringLength]`
Defines the maximum and minimum length of a string property.
public class Product
{
          [StringLength(50, MinimumLength = 3)]
          public string Name { get; set; }
}
Â
Â
3. `[Range]`
Restricts a property to a specified numeric range.
public class Temperature
{
          [Range(-40, 100)]
          public int Celsius { get; set; }
}
Â
Â
4. `[RegularExpression]`
Validates a property against a regular expression pattern.
public class Email
{
          [RegularExpression(@"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$")]
          public string Address { get; set; }
}
Â
Â
5. `[Compare]`
Compares the values of two properties for equality.
public class User
{
          public string Password { get; set; }
Â
          [Compare("Password")]
          public string ConfirmPassword { get; set; }
}
Â
Â
6. `[EmailAddress]`
Ensures that a string property contains a valid email address.
public class Contact
{
          [EmailAddress]
          public string Email { get; set; }
}
Â
Â
7. `[Phone]`
Validates a string property as a phone number.
public class Contact
{
          [Phone]
          public string PhoneNumber { get; set; }
}
Â
Â
8. `[Url]`
Check if a string property contains a valid URL.
public class Website
{
          [Url]
          public string URL { get; set; }
}
Â
Â
9. `[CreditCard]`
Ensures a string property contains a valid credit card number.
public class Payment
{
          [CreditCard]
          public string CardNumber { get; set; }
}
Â
Â
10. `[MaxLength]`
Specifies the maximum length of a string property.
public class Tweet
{
          [MaxLength(280)]
          public string Text { get; set; }
}
Â
Â
11. `[MinLength]`
Sets the minimum length for a string property.
public class Password
{
          [MinLength(8)]
          public string Value { get; set; }
}
Â
Â
12. `[DataType]`
Specifies the data type of a property.
public class Person
{
          [DataType(DataType.Date)]
          public DateTime BirthDate { get; set; }
}
Â
Â
13. `[Display]`
Customizes the display name for a property.
public class Product
{
          [Display(Name = "Product Name")]
          public string Name { get; set; }
}
Â
Â
14. `[ScaffoldColumn]`
Hides a property from a scaffolding in ASP.NET MVC.
public class Employee
{
          [ScaffoldColumn(false)]
          public int EmployeeID { get; set; }
}
Â
Â
15. `[ScaffoldTable]`
Specifies the table name for an Entity Framework Entity.
[ScaffoldTable("tbl_Product")]
public class Product
{
          public string Name { get; set; }
}
Â
Â
16. `[Editable]`
Determines whether a property is editable in MVC.
[Editable(false)]
public class UserProfile
{
          public string FirstName { get; set; }
}
Â
Â
17. `[Key]`
Marks a property as the primary key for an Entity Framework Entity.
public class Customer
{
          [Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] Â //optional
          public int CustomerID { get; set; }
}
Â
Â
18. `[ForeignKey]`
Defines a foreign key relationship in Entity Framework.
public class Order
{
          public int CustomerID { get; set; }
Â
          [ForeignKey("CustomerID")]
          public Customer Customer { get; set; }
}
Â
Â
19. `[Table]`
Specifies the table name for an Entity Framework entity.
[Table("Orders")]
public class Order
{
          public string OrderName { get; set; }
}
Â
Â
20. `[Column]`
Defines the column name for a property in Entity Framework.
public class User
{
          [Column("Full_Name")]
          public string FullName { get; set; }
}
Â
Â
21. `[ConcurrencyCheck]`
Indicates that a property should be included in optimistic concurrency checks.
public class Product
{
          [ConcurrencyCheck]
          public int UnitsInStock { get; set; }
}
Â
Â
22. `[Timestamp]`
Specifies that a property represents a database-generated timestamp.
public class Product
{
          [Timestamp]
          public byte[] RowVersion { get; set; }
}
Â
Â
23. `[DatabaseGenerated]`
Marks a property as database-generated.
public class Order
{
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int OrderID { get; set; }
}
Â
Â
24. `[NotMapped]`
Excludes a property from being mapped to the database.
public class User
{
          [NotMapped]
          public string FullName => $"{FirstName} {LastName}";
}
Â
Â
25. `[Bind]`
Specifies which properties should be included when binding data in an MVC action.
[Bind(Include = "FirstName, LastName, Email")]
public class UserProfile
{
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public string Email { get; set; }
}