【问题标题】:Updating datatable to multiple tables in entity framework将数据表更新为实体框架中的多个表
【发布时间】:2015-08-20 17:25:55
【问题描述】:

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

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

谢谢

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

【问题讨论】:

  • 取消注释该代码会发生什么?您是否遇到异常,是否成功并且更改不会持久化...发布这两个实体的类结构也可能会有所帮助。
  • 另请注意,在完成后处理数据库上下文(代码中的db 变量)很重要。通常的方法是使用 using 块。
  • @drneel 当我取消注释它运行但没有表填充数据的代码时,目前数据确实填充在学生表中。您的意思是按类结构发布模型吗?
  • 发布以下课程:ModuleEntities、Students 和 Module。学生或模块都不会持续存在,还是只是两者之一?
  • @drneel ModuleEntities 是我从中引用表的数据库的名称,似乎学生是我可以填充的唯一表。值得一提的是,我之前使用 CSV 文件填充了我的数据表?我添加的三个类是我希望填充的

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


【解决方案1】:

以下是使用实体框架将相关数据添加到数据库的要点:

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),我的模型类中是否缺少某些东西或者我的数据库集搞错了吗?
  • 我在 StudentCourses 或 StudentModule(或连接表)中注意到,如果表中除了主键字段(如表 id)之外还有任何其他字段,EF 将无法正确生成不明原因。还要确保连接表中的主键将两个字段都作为键。最后,EF 做对的一个指标是您不会看到连接表出现在 EF 关系图中。会推断出来的。因此,换句话说,您会看到 Student 和 Module,但您不应该在 EF 图中看到 StudentModule 类。
  • 抱歉,回复晚了,我的表设置正确,但添加到数据库时仍然遇到问题,当我在 dataContext 添加断点时,一切似乎都已到位。 Students.Add(student1) 阶段,我还有什么需要更改的吗?
  • 最终得到了这个工作,谢谢,发现我在多次失败的情况下添加了模块主键
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 2013-06-24
  • 1970-01-01
  • 2022-01-23
  • 2015-08-17
  • 2014-03-02
相关资源
最近更新 更多