ef code first - Entity Framework: How to Insert and Update entity with one-to-many relationship? -


we are using ef-6 code-first approach, , having trouble one-to-many relationship.

we have following entities in database gives related data:

  • public class university { public int uid { get; set; } public string name { get; set; } }

    1. public class subject{ public int subjectid { get; set; } public string name { get; set; } }
    2. public class specializedsubject{ public int specializedsubjectid { get; set; } public int subjectid { get; set; } public string name { get; set; } }
    1. public class maleteacher{ public int maleteacherid { get; set; } public string name { get; set; } }
    2. public class universitymaleteacher{ public int universitymaleteacherid { get; set; } public int maleteacherid { get; set; } public int uid { get; set; } public string name { get; set; } }
    3. public class universitymaleteachersubject{ public int universitymaleteachersubjectid{ get; set; } public int universitymaleteacherid { get; set; } public int subjectid { get; set; } public string subjectname { get; set; } }
    4. public class universitymaleteacherspecializedsubject{ public int universitymaleteacherspecializedsubjectid { get; set; } public int universitymaleteacherid { get; set; } public int specializedsubjectid { get; set; } public string subjectname { get; set; } }
    1. public class femaleteacher{ public int femaleteacherid { get; set; } public string name { get; set; } }
    2. public class universityfemaleteacher{ public int universityfemaleteacherid { get; set; } public int femaleteacherid { get; set; } public int uid { get; set; } public string name { get; set; } }
    3. public class universityfemaleteachersubject{ public int universityfemaleteachersubjectid{ get; set; } public int universityfemaleteacherid { get; set; } public int subjectid { get; set; } public string subjectname { get; set; } }
    4. public class universityfemaleteacherspecializedsubject{ public int universityfemaleteacherspecializedsubjectid { get; set; } public int universityfemaleteacherid { get; set; } public int specializedsubjectid { get; set; } public string subjectname { get; set; } }

now, have excel documents ready load data database. point note here have data loaded in database. want loop through data in excel , check if there in database; if have update else have insert it. while inserting want make sure don't insert duplicate child data ex. possible might have [subject],[specializedsubject] , [maleteacher] or [femaleteacher] etc.

i tried below traditional foreach approach iterate on collections , see if have data in db, if yes insert or update looks ugly , complex have iterate on childerens of parent.

var result= context.university .include(s => s.maleteacher.select(sm => sm.universityfemaleteacher.select(sp => sp.universityfemaleteachersubject.select(spa =>    spa.universityfemaleteacherspecializedsubject)))) .asnotracking() .singleordefault(y => (y.name == datalist.name));  if (result == null)     //insert foreach (var u in result.where(x=> x.university != null)) {     var un = //mapper code comes here gives university entity viewmodel     dbcontext.university.add(un);     dbcontext.savechanges();     //universityfemaleteacher     foreach (var um in u.universityfemaleteacher)     {         var x= //mapper code comes here gives universityfemaleteacher entity viewmodel         x.university= un;         dbcontext.universityfemaleteacher.add(x);         dbcontext.savechanges();     } } else{     //update     //using "n" foreach loops , "m" number of counter variables update records     //for updating records doing below "n" loops       var temp=dbcontext.universitymaleteacher.find(id)      temp.name="setvaluehere"enter code here      dbcontext.savechanges();      //note: doing same thing childerens } 

is there better solution solve this? appriciate if can provide better alternative.

thank you!!


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -