Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. # IT:AD:Code First:DataAnnotations # <callout type="Navigation" class="small"> * [[../|(UP)]] {{indexmenu>.#2|nsort tsort}} </callout> ## Summary ## >Important: the use of `DataAnnotations` is not recommended in anything but a POC. * Domain Entities should be in their own assembly. * Decorating domain [[IT/#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 ## Resources ## * [[IT/AD/Data/Annotations/]] * [Override DisplayName](http://stackoverflow.com/questions/356464/localization-of-displaynameattribute) * [Relationship between DisplayName and Display](http://www.dotnetcurry.com/ShowArticle.aspx?ID=715) * http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions(v=vs.103).aspx /home/skysigal/public_html/data/pages/it/ad/code_first/dataannotations.txt Last modified: 2023/11/04 03:22by 127.0.0.1