Entity Core 2.0 Model With all data annotations (Primary Key, Foreign Key,Identity,Table Name And Schema
Here is the example for EF core 2.0 model with all data annotations.
Primarykey Table:
Foreign Key Table:
Primarykey Table:
[Table("Master", Schema = "Admin")]
public class Master : BaseEntity
{
[Key] -->Primary key
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] -->Identity
[Column("Pkey_master",
Order = 0)] -->DB Column Name and Order for column
public override int
Pkey { get; set;
}
[Column(TypeName = "varchar(50)", Order = 1)] -->Column datatype
[StringLength(50, ErrorMessage = "test msg"), Required] --> datalenth
[Display(Name = "Master Name", Prompt = "Master Name")]-->for UI Disply name, placeholder
public string
Name { get; set;
}
[Column(TypeName = "varchar(50)", Order = 2)]
[StringLength(50), Required]
[Display(Name = "Comments", Prompt = "Comments")]
public string
Comments { get; set;
}
public
ICollection<Tier> Tier { get; set;
}
}
Foreign Key Table:
[Table("Tier", Schema = "Admin")]
public class Tier : BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("Pkey_tier",
Order = 0)]
public override int
Pkey { get; set;
}
[Column(TypeName = "varchar(50)")]
[StringLength(50), Required]
[Display(Name = "Tier1 Name", Prompt = "Tier1
Name")]
public string
Name { get; set;
}
[Column(TypeName = "varchar(150)")]
[StringLength(150), Required]
[Display(Name = "Comments", Prompt = "Comments")]
public string
Comments { get; set;
}
[Required]
[Column(TypeName = "Bit")]
public bool
Status { get; set;
}
[Required, Display(Name = "Master")]
[ForeignKey("Master")]
public int
Fkey_master { get; set;
} --> Foreign Key
public
Master Master { get; set;
} --> Primary Key table
}
Post a Comment