none
Adding Child entities yields an exception ... :-s RRS feed

  • Question

  • I am new to C# and LINQ to SQL ... I have the following entity relationships ...

    DbCollaboration
    +---DbSchedule
    +---DbOccurrence

    I am dumping data gathered from the web in xml for with a class hierarchy like this:

    WbxEvent
    +---WbxConference
    +---WbxOccurrence

    The two models emulate each other, the Linq objects are those beginning with Db.

    I have the following code to TRY to attempt to create the objects in the Db* Entities, however I get an "Object is not set to an object" exception at the indicated line:

     

    foreach (WbxEvent e in webexEvents)

     

        //set the collaboration data from the webex event

     

        DbCollaboration cb = new DbCollaboration(e);

     

     

        //get the schedules

     

        if (e.Conference != null)

     

            DbSchedule s = new DbSchedule(e.Conference);

     

        //get the occurrences

     

        foreach (WbxConferenceHistory h in e.History)

     

            DbOccurrence o = new DbOccurrence(h);

            cb.DbOccurrences.Add(o);  //<<<-- Exception is raised here (note, the "get conference" portion was skipped)

        }

    }

    Any help would be appreciated.  I am unsure what would be the most helpful for evaluation of my issues, so if you need additional info, don't hesitate to ask and I will provide that information.

    The Ex.Message is:
    Object reference not set to an instance of an object.

        {

     

            s.DbCollaboration = cb;

            cb.DbSchedules.Add(s);

        }

     

        {

     

    {

     

    //get the web ex stuff into the database


    Brent Spaulding | Access MVP
    Wednesday, November 18, 2009 10:38 PM

Answers

  • Hi Brent,

    Can you confirm that in your constructor for DbCollaboration that you are initializing the DbOccurrences collection?

    If not, you may need to do a cb.DbOccurrences = new [something]() before calling Add.

    Hope this helps.

    Cheers,
    -jc

    Me, coding and stuff: Mr. James
    Wednesday, November 18, 2009 10:50 PM

All replies

  • Hi Brent,

    Can you confirm that in your constructor for DbCollaboration that you are initializing the DbOccurrences collection?

    If not, you may need to do a cb.DbOccurrences = new [something]() before calling Add.

    Hope this helps.

    Cheers,
    -jc

    Me, coding and stuff: Mr. James
    Wednesday, November 18, 2009 10:50 PM
  • Hello James,

    Yep ... that was it! ... I overloaded the constructor for DbCollaboration (and the other entities) and forgot to intialize the entity sets.  Something like this for DbCollaboration:

     

    this._DbSchedules = new EntitySet<DbSchedule>(new Action<DbSchedule>(this.attach_DbSchedules), new Action<DbSchedule>(this.detach_DbSchedules));

     

    this._DbOccurrences = new EntitySet<DbOccurrence>(new Action<DbOccurrence>(this.attach_DbOccurrences), new Action<DbOccurrence>(this.detach_DbOccurrences));

     

    this._DbProvider = default(EntityRef<DbProvider>);

    OnCreated();


    Thank You James!!! ...


    Brent Spaulding | Access MVP
    Wednesday, November 18, 2009 11:20 PM