I am having an issue with my database model. I am fairly new to this, thus sorry in advance. Here are how my auto generated classes look like...
Session Model
public partial class Session
{
public Session()
{
this.Tutors = new HashSet<Tutor>();
}
public int SessionId { get; set; }
public int StudentStudentId { get; set; }
public virtual Student Student { get; set; }
public virtual ICollection<Tutor> Tutors { get; set; }
}
Student Model
public partial class Student
{
public Student()
{
this.Sessions = new HashSet<Session>();
}
public int StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Session> Sessions { get; set; }
}
Controller
public Student Get(int id)
{
using(var db = new studytree_dbEntities())
{
Student s = db.Students.FirstOrDefault(i => i.StudentId == id);
return s;
}
}
I know what the problem is. The database queries from the student table receiving the data. Since it has a sessionid column, it goes to the session table and queries the session. But since it is a one to many relationship, the session table has one student, thus a loop is created. A student grabs it session and the session grabs its student etc.... How can I make it so that it only goes one level deep. What I want to be able to do is query a student and get all its properties.