Updating data for multiple tables in an entity structure

I created a method for adding data from datatable to a table in my database through the framework entity, but this only allows me to populate one table, and I want to populate several tables within the same method.

Below, my method is added to the students table, and I commented on the modules table, because when I add this, the data is not transferred to the database. Can someone recommend a way that I can populate several tables in this method?

thanks

public Boolean ConvertDataTable(DataTable tbl) { string Feedback = string.Empty; ModuleEntities db = new ModuleEntities(); // iterate over your data table foreach (DataRow row in tbl.Rows) { student st = new student(); st.student_no = row.ItemArray.GetValue(0).ToString(); st.surname = row.ItemArray.GetValue(1).ToString(); st.firstname = row.ItemArray.GetValue(2).ToString(); db.students.Add(st); //module mod = new module(); //mod.module_code = row.ItemArray.GetValue(3).ToString(); //mod.name = row.ItemArray.GetValue(4).ToString(); //db.modules.Add(mod); } try { db.SaveChanges(); Feedback = "Upload Successful"; return true; } catch { Feedback = "Upload Failed"; return false; } } 

Student class

 public partial class student { public student() { this.submits = new HashSet<submit>(); this.takes = new HashSet<take>(); this.lecturers = new HashSet<lecturer>(); } public string student_no { get; set; } public string surname { get; set; } public string firstname { get; set; } public virtual ICollection<submit> submits { get; set; } public virtual ICollection<take> takes { get; set; } public virtual ICollection<lecturer> lecturers { get; set; } } 

Module class

 public partial class module { public module() { this.takes = new HashSet<take>(); this.lecturers = new HashSet<lecturer>(); } public string module_code { get; set; } public string name { get; set; } public virtual ICollection<take> takes { get; set; } public virtual ICollection<lecturer> lecturers { get; set; } } 

Take class

 public partial class take { public string student_no { get; set; } public string module_code { get; set; } public string grade { get; set; } public virtual module module { get; set; } public virtual student student { get; set; } } 
+7
c # asp.net-mvc datatable entity-framework
source share
1 answer

Here's the essence of adding related data to a database using the Entity Framework:

 class Program { static void Main(string[] args) { SchoolDBEntities dataContext = new SchoolDBEntities(); //Create student object Student student1 = new Student(); student1.StudentName = "Student 01"; //Create course objects Cours mathCourse = new Cours(); mathCourse.CourseName = "Math"; Cours scienceCourse = new Cours(); scienceCourse.CourseName = "Science"; //Save courses to student 1 student1.Courses.Add(mathCourse); student1.Courses.Add(scienceCourse); //Save related data to database //This will automatically populate join table //between Students and Courses dataContext.Students.Add(student1); dataContext.SaveChanges(); } } 

Another illustration: enter image description here

+1
source share

All Articles