C# 在实体框架中将datatable更新为多个表
我创建了一个方法,通过entity framework将数据表中的数据添加到数据库中的表中,但是它只允许我填充一个表,并且我希望在一个方法中填充多个表 下面是我添加到student表的方法,我已经注释掉了module表,因为当我添加这个表时,没有数据传输到数据库。有人能推荐一种在这个方法中填充多个表的方法吗 谢谢C# 在实体框架中将datatable更新为多个表,c#,asp.net-mvc,entity-framework,datatable,C#,Asp.net Mvc,Entity Framework,Datatable,我创建了一个方法,通过entity framework将数据表中的数据添加到数据库中的表中,但是它只允许我填充一个表,并且我希望在一个方法中填充多个表 下面是我添加到student表的方法,我已经注释掉了module表,因为当我添加这个表时,没有数据传输到数据库。有人能推荐一种在这个方法中填充多个表的方法吗 谢谢 public Boolean ConvertDataTable(DataTable tbl) { string Feedback = string.Empty;
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;
}
}
学生班
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 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();
}
}
另一个例子:
取消注释该代码时会发生什么?您是否遇到异常,异常是否成功,更改是否不会持久化。。。发布这两个实体的类结构也可能会有所帮助。另外请注意,在处理完数据库上下文(
DB
代码中的变量)后,处理它非常重要。通常的方法是使用using块。@drneel当我取消注释代码时,它确实运行,但是没有表填充数据,目前数据确实填充到学生表中。你的意思是按班级结构发布模型吗?发布班级:模块素质、学生和模块。学生或模块是否都不会持久化,或者只是其中一个?@drneel modulentities是我引用表的数据库名称,似乎学生是我唯一可以填充的表。值得一提的是,我以前用CSV文件填充过我的数据表吗?我添加的三个类是我希望通过银行进行回复的,我尝试了你的方法,但我无法直接将学生链接到模块(例如student.module.add),我的模型类中是否缺少某些内容,或者我的数据库设置是否有误?我在StudentCourses或StudentModule(或联接表)中注意到如果表中除了主键字段(如表id)之外还有其他字段,EF将无法正常生成,原因未知。还要确保联接表中的主键将两个字段都作为键。最后,EF做得对的一个指标是,您不会看到联接表出现在EF关系图中。这是可以推断的。因此,换句话说,您将看到Student和Module,但不应该在EF图中看到StudentModule类。很抱歉,我现在的表设置正确,但在添加到数据库时仍然遇到问题,当我在dataContext.Students.add(student1)阶段添加断点时,一切似乎都已就绪,还有什么我需要更改的吗?最终成功了谢谢,我发现我在多次添加模块主键时失败了
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; }
}
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();
}
}