Loosing Connection to linked objects in a list RRS feed

  • Question

  • Also posted on SO with no answers....

    This is a tough one to describe. I have a DB with table Tests and table students, and ScoreCards. Each test has a FK of a student, same goes to scorecard.

    Student can take the test more than once, so there can be many records of tests that points to the same student.

    I currently use the scorecard only in memory. I have a list of those scorecard objects: liScoreCard. I iterate through all the tests, each iteration I create a new scorecard object, and finally - if it's the best - I add it to the list (if not, I just change the number of tests on the already existing score card in the list):


    if the student has only one test, the SC in the list contains the Student Object (when I get the list of test I use "Include("Student")" in the query.

    if in a future test I find the same student again (in a later test) when I do this: (Adding the student object from the test to the new scorecard object) sc.Student = test.Student

    in the list: liScoreCards - the previous SC object.Student becomes NULL instead of the Student object that was there.

    What am I doing wrong ?

    Is it possible that assigning the student object to the new ScroeCard object "removes" it from the old scorecard object stored in the list ?

    code sample:

     // Psuedo code:
     liTest = DB.GetTests().Include("Student");
     // Code: 
       List<ScoreCard> liScoreCard = new List<ScoreCard>();
        foreach(Test st in liTest)
    ScoreCard SC = new ScoreCard();
    SC.Student = st.Student;        // This line makes SC with the same StudentID in liScoreCard have the Student become null.
    ScoreCard oldSC = liScoreCard.Find(x->x.StudentID == SC.StudentID);
    // Some logic to see which grade is higher)
    // either update oldSC or...

    Monday, February 25, 2013 9:00 AM

All replies

  • Hi,

    Have you checked that test.Student is not null in which case you would update an old entry with null ? (also not sure but my understanding is that you want to update the scorecard, not the student to which belongs this score card as this is already the same student ?)

    At one point, you'll likely have to provide the smallest amount of compilable code that shows the issue. Either you'll find the issue or you'll have some short code to show (if you post tons of code unrelated to your issue, it's likely it will be just ignored). For now it seems we don't have enough info to find the place where you have something wrong.

    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Monday, February 25, 2013 11:01 AM
  • Hi

    ScoreCard is a new object.

    I create it, than I put some data inside, including the StudentID and the actual Student object.

    It works good until I get another test from an already tested student. when I set the new ScoreCard object with the same stuent object (which is from the new Test Object) it sets the old ScoreCard's Student object to null.

    The test object has a student value....

    I've tried to generate a new student object but than it gets "angry" with me for mixing contexts...

    (I will try to create a sample, this is a part of a big project... and it is obviously not students and tests... but I will try to create a simple sample of this issue).
    • Edited by Daniel-IL Monday, February 25, 2013 3:13 PM
    Monday, February 25, 2013 3:08 PM
  • Hi Daniel-IL;

    It would help to see the code and indicate on what line the issue is occurring on. Also when this happens, "when I set the new ScoreCard object with the same stuent object (which is from the new Test Object) it sets the old ScoreCard's Student object to null.", is that being done with the same DbContext instance as the first one?


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, February 25, 2013 3:24 PM