Mayooran Navamany

Mayooran Navamany

  • 324
  • 5.6k
  • 133.1k

one to many Relationships In Entity Framework Core

Aug 10 2022 3:40 PM

One too many Relationships In the .net core a class have many students in my cause I could not get the student's class name from students table 

public class Student
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(100)]
        public string FirstName { get; set; }
        [Required]
        [StringLength(100)]
        public string LastName { get; set; }
        [Required]
        [StringLength(200)]
        public string ContactPerson { get; set; }
        [Required]
        public string ContactNo { get; set; }
        [Required]
        [EmailAddress]
        public string Email { get; set; }
        [Required]
        public string DateOfBirth { get; set; }

        //Navigations Properties
        [Required]
        public int? ClassId { get; set; }
        public Class Class { get; set; }


    }

public class Class
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [StringLength(100)]
        public string Name { get; set; }
        public ICollection<Student> Students { get; set; }

}

 


Answers (3)