Updated on Kisan Patel
To demonstrate Primarykey and foreignkey relationship we need two models with primarykey defined and foreignkey defined in them.
we will use
1) parentCategory – Which has Id which is PrimaryKey
2) ChildCategpory – Which has foreign key as “ParentID” that is related with “Id” of ParentCategory table
Now we Create proper model for your database
public class parentCategory { [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] public string ParentName { get; set; } } public class ChildCategpory { [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int ChildId { get; set; } [Required] public string ChildName{ get; set; } // Foreign key [Display(Name = "Parent Name")] public int ParentId { get; set; } [ForeignKey("ParentId")] public virtual ParentCategory Parent { get; set; } }