0% found this document useful (0 votes)
3K views3 pages

11.1 Data Annotations Cheat Sheet

The document discusses various data annotation attributes in .NET that can be applied to classes and properties to specify metadata for entity mapping and validation. It provides examples of attributes for tables, columns, primary keys, composite primary keys, indices, composite indices, and foreign keys. The attributes allow specifying metadata like schema, data type, required fields, length validation, column ordering in composite keys, index uniqueness, and relationships between entities.

Uploaded by

Luis Palmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views3 pages

11.1 Data Annotations Cheat Sheet

The document discusses various data annotation attributes in .NET that can be applied to classes and properties to specify metadata for entity mapping and validation. It provides examples of attributes for tables, columns, primary keys, composite primary keys, indices, composite indices, and foreign keys. The attributes allow specifying metadata like schema, data type, required fields, length validation, column ordering in composite keys, index uniqueness, and relationships between entities.

Uploaded by

Luis Palmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Data Annotations By: Mosh Hamedani

Tables
[Table(“tbl_Course”,  Schema  =  “catalog”  )]  
public  class  Course

{

}  

Columns
[Column(“sName”,  TypeName  =  “varchar”)]  
[Required]  
[MaxLength(255)]  
public  string  Name  {  get;  set;  }


Primary Keys
[Key]  
[DatabaseGenerated(DatabaseGeneratedOption.None)]  
public  class  Isbn  {  get;  set;  }


1
Data Annotations By: Mosh Hamedani

Composite Primary Keys


public  class  OrderItem  

{  
       [Key]  
       [Column(Order  =  1)]  
       public  int  OrderId  {  get;  set;  }


       [Key]  
       [Column(Order  =  2)]  
       public  int  OrderItemId  {  get;  set;  }


}  

Indices
[Index]  
public  int  AuthorId  {  get;  set;  }


[Index(IsUnique  =  true)]  
public  string  Name  {  get;  set;  }


2
Data Annotations By: Mosh Hamedani

Composite Indices
[Index(“IX_AuthorStudentsCount”,  1)]  
public  int  AuthorId  {  get;  set;  }


[Index(“IX_AuthorStudentsCount”,  2)]  
public  int  StudentsCount  {  get;  set;  }


Foreign Keys
public  class  Course  

{  
       [ForeignKey(“Author”)]  
       public  int  AuthorId  {  get;  set;  }


       public  Author  Author  {  get;  set;  }  


}  

You might also like