IT:AD:Code First:DataAnnotations
Summary
Important: the use of `DataAnnotations` is not recommended in anything but a POC.
Domain Entities should be in their own assembly. Decorating domain Entities/ with Attributes drag in – and therefore to all assemblies referring to domain entities (ie everywhere) – creates brittle cod e(see: IT:AD:Patterns:Vendors Suck Strategy). Prefer the fluent approach: IT:AD:Code First:FluentAPI
Not only do they not cause dependencies, but the Fluent approach offers more options.
#
# Attributes ##
That said, the most common EF Attributes one can apply are:
[Key]– Column(s) that define the Primary Key in the database table[StringLength]– Minimal and maximal string length (only used for validation purposes not as database constraints)[MaxLength]– Can be used instead of[StringLength]to just define the maximal string length of a column[ConcurrencyCheck]– Flag that indicates that concurrency checks are activated for the column[Required]– Flag that indicates that a value is required for the column (column is flagged as not null)[Timestamp]– Flag on time stamp columns which is used for concurrency checks[Column]– Specify the name of a column (default property name), can also be used to define the column position[Table]– Specify the name of a table (default class name)[DatabaseGenerated]– Flag that indicates that the value is filled by the database (possible values are Computed, Identity, None)[NotMapped]– Is used to define a property in the class that does not get generated in the database.[ForeignKey]and[InverseProperty]– Column(s) that are defined as Foreign Keys