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









up vote
0
down vote

favorite












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.



This is my sample code using 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. While Subject_Schedules and Subject_Department inherits Subject_Curriculum's primary key. What can I do to make this insertion possible to all table at once?










share|improve this question























  • Do it in 1 transaction?
    – ntohl
    Nov 10 at 21:10














up vote
0
down vote

favorite












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.



This is my sample code using 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. While Subject_Schedules and Subject_Department inherits Subject_Curriculum's primary key. What can I do to make this insertion possible to all table at once?










share|improve this question























  • Do it in 1 transaction?
    – ntohl
    Nov 10 at 21:10












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.



This is my sample code using 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. While Subject_Schedules and Subject_Department inherits Subject_Curriculum's primary key. What can I do to make this insertion possible to all table at once?










share|improve this question















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.



This is my sample code using 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. While Subject_Schedules and Subject_Department inherits Subject_Curriculum's primary key. What can I do to make this insertion possible to all table at once?







c# linq foreign-keys






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 22:32









marc_s

566k12610921245




566k12610921245










asked Nov 10 at 20:12









Joshua Paredes

286




286











  • Do it in 1 transaction?
    – ntohl
    Nov 10 at 21:10
















  • Do it in 1 transaction?
    – ntohl
    Nov 10 at 21:10















Do it in 1 transaction?
– ntohl
Nov 10 at 21:10




Do it in 1 transaction?
– ntohl
Nov 10 at 21:10












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243002%2fhow-can-i-insert-more-than-2-tables-in-linq-using-foreign-keys%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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.






    share|improve this answer
























      up vote
      0
      down vote













      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.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 21:12









        Joshua Paredes

        286




        286



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243002%2fhow-can-i-insert-more-than-2-tables-in-linq-using-foreign-keys%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線