简体   繁体   English

如何使用外键在LINQ中插入两个以上的表

[英]How can I insert more than 2 tables in LINQ using foreign keys

I am apparently Inserting data using LINQ by creating classes of tables in the databases but it just has error that says object is null. 我显然是通过在数据库中创建表的类来使用LINQ插入数据,但是它只是显示对象为null的错误。

This is my sample code using C# LINQ: 这是我使用C#LINQ的示例代码:

using (dc = new linqDBDataContext(conn))
{
    Subject_Curriculum sc;
    Subject_Schedule ss;
    Subject_Department sd;
    Subject_Standing sst;

    Pre_Requisite pr;
    Pre_Requisite_Year_Standing prys;

    Curriculum cu = new Curriculum();
    cu.Curriculum_Title = curriculumName;
    cu.Course_Number = courseNumber;

    foreach (var s in ssd)
    {
        sc = new Subject_Curriculum();
        sc.Course_Code = s.courseCode;
        sc.Course_Title = s.courseTitle;
        cu.Subject_Curriculums.Add(sc);
        dc.Subject_Curriculums.InsertOnSubmit(sc);

        for (int i = 0; i < s.numberOfSchedules; i++)
        {
            ss = new Subject_Schedule();

            if (i == 0)
            {
                ss.Units = s.unitsLec;
                ss.Schedule_Type = "Lecture";
                ss.Number_Of_Hours = s.numberOfHoursLec;
            }
            else
            {
                ss.Units = s.unitsLab;
                ss.Schedule_Type = "Laboratory";
                ss.Number_Of_Hours = s.numberOfHoursLab;
            }

            sc.Subject_Schedules.Add(ss);
            dc.Subject_Schedules.InsertOnSubmit(ss);
        }

        foreach (var sdl in s.department)
        {
            sd = new Subject_Department();
            sd.Department_Number = sdl;
            sc.Subject_Departments.Add(sd);
            dc.Subject_Departments.InsertOnSubmit(sd);
        }

        sst = new Subject_Standing();
        sst.Year = s.year;
        sst.Semester = s.semester;

        cu.Subject_Standings.Add(sst);
        dc.Subject_Standings.InsertOnSubmit(sst);

        if (s.yearStandingStatus)
        {
            prys = new Pre_Requisite_Year_Standing();
            prys.Year_Standing = Convert.ToInt32(s.yearStanding.ToString().Substring(0, 1));
            sc.Pre_Requisite_Year_Standings.Add(prys);
            dc.Pre_Requisite_Year_Standings.InsertOnSubmit(prys);
        }
        else
        {
            if (s.prereq.Count == 0)
            {
                pr = new Pre_Requisite();
                pr.Pre_Requisite_Code = null;
                sc.Pre_Requisites.Add(pr);
                dc.Pre_Requisites.InsertOnSubmit(pr);
            }
            else
            {
                foreach (var p in s.prereq)
                {
                    pr = new Pre_Requisite();
                    pr.Pre_Requisite_Code = Convert.ToInt32(p);
                    sc.Pre_Requisites.Add(pr);
                    dc.Pre_Requisites.InsertOnSubmit(pr);
                }
            }
        }
    }

    dc.Curriculums.InsertOnSubmit(cu);
    dc.SubmitChanges();

    return true;
}

As you can see in the code, the Curriculum table has the highest hierarchy in the database and the other tables inherits its primary key into Subject_Curriculum, Pre_Requisite, Subject_Standing and Pre_Requisite_Year_Standing. 从代码中可以看到, Curriculum表在数据库中具有最高的层次结构,其他表将其主键继承到Subject_Curriculum,Pre_Requisite,Subject_Standing和Pre_Requisite_Year_Standing中。 While Subject_Schedules and Subject_Department inherits Subject_Curriculum's primary key. 而Subject_Schedules和Subject_Department继承Subject_Curriculum的主键。 What can I do to make this insertion possible to all table at once? 我该怎么做才能一次插入所有表?

I already solved my question. 我已经解决了我的问题。 It is just by adding all tables from their foreign keys and insert and submit changes at the end of the loop. 只需通过外键添加所有表,然后在循环末尾插入并提交更改。 This makes this thread close. 这使该线程关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM