Data Annotation Attribute On Code First Approach

Data Annotation Attribute On Code First Approach

  1. Key: This is user to make the primary key on the Data Table
  2. TimeStamp
  3. ConcurrencyCheck: In Case of Update command In Entity Framework takes value of this column in the Where Clause.
  4. Required: Entity Framework Create a Not null Property in the database.
  5. Max/min LengthIt can be applied only string or array type of property in Model Class.
  6. StringLengthCode first create a fix size of column in the string length. Here is very interesting point that then whats the use of max length. According to me the maxlength is used to create the column in DB while Stringlength is used for client side validation.
  7. TableAttibute is used to create the Table with the specified name.
  8. ColumnCreate a column with that name (default is model property name). You can also specify an order and type of the column using Column attribute.
  9. ForeignKeyBy default the Foreignkey is set by the reference property.
    1. //Foreign key for Standard  
    2. public int StandardId   
    3. {  
    4.     get;  
    5.     set;  
    6. }  
    7.   
    8. public Standard Standard  
    9. {  
    10.     get;  
    11.     set;  
    12. }  
    13. public int StandardId   
    14. {  
    15.     get;  
    16.     set;  
    17. }  
    18. public string StandardName  
    19. {  
    20.     get;  
    21.     set;  
    22. }  
    23. // But the Foreign key DataAnnotation can change the default behaviour  
    24. public class Student  
    25. {  
    26.     public Student()  
    27.     {  
    28.   
    29.     }  
    30.     public int StudentID   
    31.     {  
    32.         get;  
    33.         set;  
    34.     }  
    35.     public string StudentName   
    36.     {  
    37.         get;  
    38.         set;  
    39.     }  
    40.     //Foreign key for Standard  
    41.     public int StandardRefId   
    42.     {  
    43.         get;  
    44.         set;  
    45.     }  
    46.     [ForeignKey("StandardRefId")]  
    47.     public Standard Standard   
    48.     {  
    49.         get;  
    50.         set;  
    51.     }  
    52. }  
    53. public class Standard  
    54. {  
    55.     public Standard()  
    56.     {  
    57.   
    58.     }  
    59.     public int StandardId   
    60.     {  
    61.         get;  
    62.         set;  
    63.     }  
    64.     public string StandardName   
    65.     {  
    66.         get;  
    67.         set;  
    68.     }  
    69.     public ICollection < Student > Students   
    70.     {  
    71.         get;  
    72.         set;  
    73.     }  
    74. }  
  10. NotMapped: Implementing this column the Default Column is not created. Code first also does not create a column for a properties which does not have either getters or setters as:
    1. public string FirstName { getreturn StudentName;} }  
    2. public string Age { set{ _age = value;} }  
  11. InverseProperty: Code First creates {Class Name}_{Primary Key} foreign key column if you have not included foreign key property in a parent class. The InverseProperty attribute is used when you have multiple relationships between classes.

    Fluent API is another way to configure your domain classes. Fluent API provides more functionalities for configuration than DataAnnotations. Fluent API supports following types of mappings.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now