Below is a simple approach to save relational database records which is working perfectly fine. I have doubt on one scenario. Before that i need to know the way i am approaching has any difficulties if the database complexity increases. Any better, efficient but simple approach?
ONE to ONE:
tb_student // store student details
id, name, country_id (country_id foriegnkey set with id of tb_country)
tb_country // store all available countries
id, name
[Table("tb_student")]
public class Student
{
[Key]
public int id { get; set; }
public string name { get; set; }
public Country country { get; set; }
}
[Table("tb_country")]
public class Country
{
[Key]
public int id { get; set; }
public string name { get; set; }
}
with
student come as parameter or create new student
Country _country = // we have selected country
StudentModelContext sdb = new StudentModelContext();
student.country = _country;
sdb.students.Add(student);
sdb.SaveChanges();
ONE to MANY:
tb_student // store student details
id, name
tb_typesubject // store all available subjects
id, name
tb_subject // store student - subject relation
id, student_id, subjecttypeid
[Table("tb_student")]
public class Student
{
[Key]
public int id { get; set; }
public string name { get; set; }
public List<Subject> subjects { get; set; }
}
[Table("tb_typesubject")]
public class TypeSubject
{
[Key]
public int id { get; set; }
public string name { get; set; }
}
[Table("tb_subject")]
public class Subject
{
[Key]
public int id { get; set; }
public int subjecttypeid { get; set; }
// we dont have to create student_id here
}
with
student come as parameter or create new student
TypeSubject _subjType1 = // we have selected subject list
TypeSubject _subjType2 = // we have selected subject list
Subject _subject1 = new Subject();
_subject1.subjecttypeid = _subjType1.id;
Subject _subject2 = new Subject();
_subject2.subjecttypeid = _subjType2.id;
StudentModelContext sdb = new StudentModelContext();
student.subjects = new List<Subject>;
student.subjects.add(_subject1);
student.subjects.add(_subject2);
sdb.students.Add(student);
sdb.SaveChanges();
This works perfectly. And i am very glad. We can load all values by
Student stud = sd.students.Find(1);
stud.Entry(stud).Collection(s => s.subjects).Load();
If student can give fees by installment for each subject
for (int i = 0; i < stud.subjects.Count; i++)
sd.Entry(stud.subjects[i]).Collection(f => f.fees).Load();
My doubt is how to design following scenerio:
There will be review for each student which is send by another student. How to do this for class like:
[Table("tb_student")]
public class Student
{
[Key]
public int id { get; set; }
public string name { get; set; }
public List<Review> reviews { get; set; }
}
[Table("tb_review")]
public class Review
{
[Key]
public int id { get; set; }
public string message { get; set; }
public int student_id { get; set; } // review of which student
public Student reviewer { get; set; } // whom send the review
}
any help ?
Try to add 2 Students in your Review class, for example:
And your Student class should be like this:
Table structure