ASP.NET MVC

Data Annotation Entity Framework

Source: Mosh Hamedani

1. Table Name

[Table("tbl_Course", Schema = "")]
public class Course

2. Column Names

[Column("sName", TypeName="varchar")]
public string Name {get; set;}

3. Primary Key

[Key]
DatabaseGenerated(DatabaseGeneratedOption.None)]
public string ISBN {get; set;}

Lưu ý: DatabaseGeneratedOption có 3 thuộc tính: None, Identity và Computed

4. Composite Keys

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

5. Not Null

[Required]
public string Name {get; set;}

6. Length of Strings

[MaxLength(255)]
public string Name {get; set;}

7. Index

[Index]
public int AuthorId {get; set;}
[Index(IsUnique = true)]
public string Email {get; set;}
[Index("IDX_AuthrStudent", 1)]
public int Author {get; set;}
[Index("IDX_AuthrStudent", 2)]
public int Student {get; set;}

8. Foreign Key


public class Course
{
[ForeignKey("Author")]
public int AuthorId {get;set}
  public Author Author {get;set}
}

or


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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s