0

I am fairly new to MVC programming, so please go gentle on me. I am developing a database, where I need to seed data with a many-to-many relationship. I have done a lot of searching on this and other fora, finding various suggestions but none seem to work in my setup. When I do the update-database command after the add-migration I keep getting an error ("Object reference not set to an instance of an object") regardless of the approaches I have tried. I have the following model classes:

public class Cognition
{
    public int CognitionID { get; set; }
    public string CognitionText { get; set; }
    public virtual ICollection<Diagnosis> Diagnoses { get; set; }
    public virtual ICollection<SubDiagnosis> SubDiagnoses { get; set; }
}
public class Diagnosis
{
    public int DiagnosisID { get; set; }
    public string DiagnosisText { get; set; }
    public virtual ICollection<Cognition> Cognitions { get; set; }
    public virtual ICollection<SubCognition> SubCognitions { get; set; }
}

I have additionally two model classes for SubCognition and SubDiagnoses (one-to-many relationships) that I have left out here for simplicity. I then have the following seeding code:

protected override void Seed(NVD.DAL.NVDContext context)
    {
        var diagnoses = new[]
        {
            new Diagnosis { DiagnosisText = "Alzheimer's disease" },
            new Diagnosis { DiagnosisText = "Mixed dementia" },
            new Diagnosis { DiagnosisText = "Cerebrovascular disease" },
            new Diagnosis { DiagnosisText = "Lewy body dementia" },
            new Diagnosis { DiagnosisText = "Parkinson's disease (PD)" },
            new Diagnosis { DiagnosisText = "Parkinson Plus (PSP/CBD/MSA)" },
            new Diagnosis { DiagnosisText = "Huntington's disease" },
            new Diagnosis { DiagnosisText = "Frontotemporal dementia" },
            new Diagnosis { DiagnosisText = "Normal pressure Hydrocephalus (NPH)" },
            new Diagnosis { DiagnosisText = "Other neurodegenerative disorder" },
            new Diagnosis { DiagnosisText = "Alcohol" },
            new Diagnosis { DiagnosisText = "Other non-neurodegenerative disorder" },
            new Diagnosis { DiagnosisText = "Unknown" },
            new Diagnosis { DiagnosisText = "SCD" },
            new Diagnosis { DiagnosisText = "Psychiatric illness" },
            new Diagnosis { DiagnosisText = "NPH" },
            new Diagnosis { DiagnosisText = "Other" }          
        };

        context.Diagnoses.AddOrUpdate(r => r.DiagnosisText, diagnoses[0], diagnoses[1], diagnoses[2], diagnoses[3], diagnoses[4], diagnoses[5], diagnoses[6], diagnoses[7], diagnoses[8], diagnoses[9],
            diagnoses[10], diagnoses[11], diagnoses[12], diagnoses[13], diagnoses[14], diagnoses[15], diagnoses[16]);

        var cognitions = new[]
        {
            new Cognition { CognitionText = "Normal" },
            new Cognition { CognitionText = "Significantly impaired" },
            new Cognition { CognitionText = "Mild dementia" },
            new Cognition { CognitionText = "Moderate dementia" },
            new Cognition { CognitionText = "Severe dementia" }
        };
        context.Cognitions.AddOrUpdate(r => r.CognitionText, cognitions[0], cognitions[1], cognitions[2], cognitions[3], cognitions[4]);
        context.SaveChanges();

        cognitions[0].Diagnoses.Add(diagnoses[14]);
        context.SaveChanges();
    }

I know that cognition[0] and diagnoses[14] are not null, since I used them elsewhere in the seeding code for adding some one-to-many-relationships, and I know that it is the last two lines of code that fails, since if I delete those everything works great. Can someone point me in the right direction as to how to get this to work?

Any help is greatly appreciated!

All the best Troels

Nielsen
  • 25
  • 5

1 Answers1

0

I think the problem is that cognitions[0].Diagnoses is null.

Add the following before cognitions[0].Diagnoses.Add(diagnoses[14]);:

cognitions[0].Diagnoses = new List<Diagnosis>();

Btw. you add diagnoses[14] twice, this is not necessary..

Michal B.
  • 5,676
  • 6
  • 42
  • 70
  • Of course! So obvious when you are told the answer. As I said, I am fairly new to this. Thanks a lot! – Nielsen May 09 '18 at 09:57