在Entity Framework中将datatable更新到多个表

9

我创建了一个方法,通过实体框架将数据从datatable添加到数据库中的表中,但它只允许我填充一个表,而我想在一个方法中填充多个表。

以下是我的方法向学生表添加内容,我已经注释掉了模块表,因为当我加入它时,没有数据传输到数据库。有人能推荐一种方法吗?我如何在这个方法中填充多个表?

谢谢

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; }
}

模块类
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; }
}

上课

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; }
}

1
当您取消对该代码的注释时会发生什么?您是否会收到异常,它是否成功并且更改未被持久化... 对于这两个实体,发布类结构也可能很有帮助。 - drneel
还要注意,当您完成使用数据库上下文(代码中的“db”变量)时,将其处理掉非常重要。通常的做法是使用 using 块来实现这一点。 - Mark Pim
@drneel ModuleEntities是我正在引用表的数据库名称,似乎只有students表可以填充。值得一提的是,我之前已经使用CSV文件填充了我的datatable吗?我添加的三个类是我希望填充的内容。 - cg91
尝试将您的代码取消注释并重新运行,然后查看输出窗口以获取可能的异常。有时异常未被抛出是因为调试异常设置的原因。 - Hopeless
检查了输出窗口并经常得到以下消息:“跳过加载符号。模块已优化,并启用调试器选项“仅限我的代码”。 - cg91
显示剩余2条评论
1个回答

0

以下是使用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();
    }
}

另一个例子: 在此输入图像描述

谢谢回复,我尝试了你的方法,但是我无法直接将学生链接到模块(例如student.module.add),我的模型类中是否有什么遗漏或者我的数据库设置有问题? - cg91
我注意到在StudentCourses或StudentModule(或连接表)中,如果除了主键字段(如表ID)之外还有其他字段,则EF无法正确生成,原因未知。同时确保连接表的主键具有两个字段作为键。最后,EF生成正确的指标是您不会在EF关系图中看到连接表的出现。它将被推断。换句话说,您将看到Student和Module,但不应在EF图中看到StudentModule类。 - Rod
抱歉回复晚了,我现在已经正确设置了我的表格,但是仍然遇到了向数据库添加数据的问题,在 dataContext.Students.Add(student1) 阶段添加断点时似乎一切都已经就位了,还需要改变其他什么吗? - cg91
最终搞定了,谢谢。我发现我在多个场合添加了模块的主键,导致失败。 - cg91

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接