将数据表更新为 entity framework 中的多个表
Updating datatable to multiple tables in entity framework
我创建了一种方法,通过 entity framework 将数据从数据 table 添加到数据库中的 table,但是它只允许我填充一个 table 我想在一个方法中填充多个 table。
下面是我添加到学生 table 的方法并且我注释掉了模块 table 因为当我添加它时没有数据传输到数据库。有人可以推荐一种我可以在此方法中填充多个 table 的方法吗?
谢谢
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;
}
}
学生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; }
}
模块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; }
}
取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; }
}
这里是使用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();
}
}
再举例说明:
我创建了一种方法,通过 entity framework 将数据从数据 table 添加到数据库中的 table,但是它只允许我填充一个 table 我想在一个方法中填充多个 table。
下面是我添加到学生 table 的方法并且我注释掉了模块 table 因为当我添加它时没有数据传输到数据库。有人可以推荐一种我可以在此方法中填充多个 table 的方法吗?
谢谢
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;
}
}
学生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; }
}
模块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; }
}
取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; }
}
这里是使用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();
}
}
再举例说明: