c# - 将数据表更新为 Entity Framework 中的多个表

标签 c# asp.net-mvc entity-framework datatable

我创建了一种方法,通过 Entity Framework 将数据表中的数据添加到我的数据库中的表中,但是它只允许我填充一个表,我想在一个方法中填充多个表。

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

谢谢

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

最佳答案

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

另一个例子: enter image description here

关于c# - 将数据表更新为 Entity Framework 中的多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32124575/

相关文章:

c# - 您是否将存储库模式与 Entity Framework 一起使用?

entity-framework - CREATE DATABASE 权限在数据库 'master' Entity Framework 迁移中被拒绝

entity-framework - 带有 Entity Framework 的 ASP Web API Controller 非常缓慢和奇怪的结果

asp.net-mvc - 不显眼的 MVC3 验证复选框组

javascript - 在 JavaScript 条件语句中使用 Razor 语法

C# 查询日期时间

c# - 单击链接按钮时清除复选框列表选择(尽管回发检查)

c# - MVC 3 在 ValidationSummary 中显示 HTML

c# - 正确的 'reactive' 方法来转换 observable 偶尔会抛出额外的值

c# - Web API Controller ,RESTful Web 方法。 (有 Angular )