asp.net mvc 4 - How to have multiple one-to-many relations between two entities using Entity Framework Code First -
below simple approach save relational database records working fine. have doubt on 1 scenario. before need know way approaching has difficulties if database complexity increases. better, efficient simple approach?
one one:
tb_student // store student details id, name, country_id (country_id foriegnkey set id of tb_country) tb_country // store 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 parameter or create new student country _country = // have selected country studentmodelcontext sdb = new studentmodelcontext(); student.country = _country; sdb.students.add(student); sdb.savechanges();
one many:
tb_student // store student details id, name tb_typesubject // store 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; } // dont have create student_id here }
with
student come parameter or create new student typesubject _subjtype1 = // have selected subject list typesubject _subjtype2 = // 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. , glad. can load values by
student stud = sd.students.find(1); stud.entry(stud).collection(s => s.subjects).load();
if student can give fees installment each subject
for (int = 0; < stud.subjects.count; i++) sd.entry(stud.subjects[i]).collection(f => f.fees).load();
my doubt how design following scenerio:
there review each student send student. how 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 student public student reviewer { get; set; } // whom send review }
any ?
try add 2 students in review class, example:
[table("tb_review")] public class review { [key] public int id { get; set; } public string message { get; set; } public student student{ get; set; } // review of student public student reviewer{ get; set; } // whom send review }
and student class should this:
[table("tb_student")] public class student { [key] public int id { get; set; } public string name { get; set; } [inverseproperty("student")] public list<review> reviewabout{ get; set; } [inverseproperty("reviewer")] public list<review> reviewby{ get; set; } }
Comments
Post a Comment