Thursday 11 June 2015

Table attribute in entity framework namespace

We need to use both two namespace  in order to use table or key attribute in entity framework.
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;


 [Table("tab_userprofle")]
    public class UserProfile
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }</div>


System.ComponentModel.DataAnnotations Attributes:

AttributeDescription
KeyMark property as EntityKey which will be mapped to PK of related table.
TimestampMark the property as a non-nullable timestamp column in the database.
ConcurrencyCheckConcurrencyCheck annotation allows you to flag one or more properties to be used for concurrency checking in the database when a user edits or deletes an entity.
RequiredThe Required annotation will force EF (and MVC) to ensure that property has data in it.
MinLengthMinLength annotation validates property whether it has minimum length of array or string.
MaxLengthMaxLength annotation maximum length of property which in turn sets the maximum length of column in the database
StringLengthSpecifies the minimum and maximum length of characters that are allowed in a data field.

System.ComponentModel.DataAnnotations.Schema Attributes:

AttributeDescription
TableSpecify name of the DB table which will be mapped with the class
ColumnSpecify column name and datatype which will be mapped with the property
IndexCreate an Index for specified column. (EF 6.1 onwards only)
ForeignKeySpecify Foreign key property for Navigation property
NotMappedSpecify that property will not be mapped with database
DatabaseGeneratedDatabaseGenerated attribute specifies that property will be mapped to Computed column of the database table. So the property will be read-only property. It can also be used to map the property to identity column (auto incremental column).
InversePropertyInverseProperty is useful when you have multiple relationships between two classes.
ComplexTypeMark the class as complex type in EF.