nullreference returned when using Subpath of PrefetchPath

Posts   
 
    
koen75
User
Posts: 7
Joined: 21-Mar-2007
# Posted on: 20-Feb-2008 19:19:11   

Hello!

I apologize if this is already answered somewhere. I apologize if this question is stupid too!

I have 3 entities: A, B and C.

The PK of entity B is a FK in entity A and C. (or in other words B has a 1:m relation to A and also to C)

The A entity is present in the EntityCollection of an LLBLGenProDatasource2 control. Because the A entity has an 'extended' custom property that uses information of related B and C entities, the following prefetchpath is contructed and assigned to the LLBLGenProDatasource2 instance :

IPrefetchPath2 prefetchPathforA = new PrefetchPath2((int)EntityType.AEntity); prefetchPathforA.Add(AEntity.PrefetchPathB).SubPath.Add(BEntity.PrefetchPathC);

Now it can happen that an instance of the B entity has no related C entities, the C property on the instance of the B entity instance in question will hold a null reference instead of an empty EntityCollection2<C>

How do I construct the prefetchpath to avoid having to check for nullreferences on the B.C property but instead to have an empty EntityCollection2<C> ?

Thank you! Koen

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Feb-2008 03:48:53   

Hi Koen,

Now it can happen that an instance of the B entity has no related C entities, the C property on the instance of the B entity instance in question will hold a null reference instead of an empty EntityCollection2<C>

The EntityCollection2<C> indeed should be an empty one, unless, of course, that the B entity would be null.

Could you please confirm that and paste the code snippet where you are checking the nullity?

David Elizondo | LLBLGen Support Team
koen75
User
Posts: 7
Joined: 21-Mar-2007
# Posted on: 21-Feb-2008 18:38:55   

Hello David:

SLQ 2005 Express edition Visual Web Developer 2005 Express Edition

LLBLGenPro 2.0 August 23rd, 2007

involved entities:

CultureEntity

CountryEntity

RegionInCountryEntity

RegionEntity

RegionLocaleEntity

relationships:

CountryEntity 1:m RegionInCountryEntity n:1 RegionEntity 1:m RegionLocaleEntity m:1 CultureEntity


default.aspx has:

LLBLProGenDataSource2 _cultureDS, binded to DropDownList_Culture LLBLProGenDataSource2 _countryDS, binded to DropDownList_Country LLBLProGenDataSource2 _regionInCountryDS, binded to DropDownList_RegionInCountry

_regionInCountryDS has as selectparameter the DropDownList_Country, so when a country is selected the child RegionInCountry entities are shown in DropDownList_RegionInCountry

Depending on the selected culture, the RegionInCountry names will be displayed in the correct language, the displayed datafield in DropDownList_RegionInCountry is the property RegionName :


The code for the extended custom property on RegionInCountry :

using System; using System.Collections.Generic; using System.Text; using nr.EntityClasses;

namespace nr.EntityClasses { public partial class RegionInCountryEntity { //expose the regionname according to the selected culture public string RegionName { get { if (this.Region != null) return this.Region.RegionName; else return "<error: No Region found.>"; //this NEVER occurs } } }

}

The code for the extended custom property on RegionInCountry :

using System; using System.Collections.Generic; using System.Text; using nr.EntityClasses;

namespace nr.EntityClasses { public partial class RegionEntity { //need to store a reference to the culture so the class is 'smart' public static int CurrentCultureId = -1;

    //expose the regionname according to the selected culture
    public string RegionName
    {
        get
        {
            //If no locales are defined a null reference will be stored!!(asked in LLBLGen forum)
            if (this._regionLocale != null) 
            {
                if (RegionEntity.CurrentCultureId != -1)
                {

                    foreach (RegionLocaleEntity regionLocal in this._regionLocale)
                    {
                        if (regionLocal.CultureId == RegionEntity.CurrentCultureId)
                            return regionLocal.RegionLocaleName;
                    }
                }               
            }

            //if no culture is selected, or no matching culture is stored,
            //the default is returned
            return this.RegionNameDefault;
        }
    }
}

}


for these extended properties to work in Page_Load the code is:

protected void Page_Load(object sender, EventArgs e) { this.DropDownList_Cultures.SelectedIndexChanged += new EventHandler(DropDownList_Cultures_SelectedIndexChanged); this.DropDownList_Countries.SelectedIndexChanged += new EventHandler(DropDownList_Countries_SelectedIndexChanged);

//page is loaded for the first time if (!Page.IsPostBack) { //CountryEntity objects have to load their related CountryLocale entities IPrefetchPath2 prefetchPathCountry = new PrefetchPath2((int)EntityType.CountryEntity); prefetchPathCountry.Add(CountryEntity.PrefetchPathCountryLocale); this._countriesDS.PrefetchPathToUse = prefetchPathCountry;

        //RegionInCountry objects have to load their related Region entities
        //and Region entities their related RegionLocaleEntities (for correct culture display)
        IPrefetchPath2 prefetchPathRegion = new PrefetchPath2((int)EntityType.RegionInCountryEntity);
                                prefetchPathRegion.Add(RegionInCountryEntity.PrefetchPathRegion).SubPath.Add(RegionEntity.PrefetchPathRegionLocale);

        this._regionInCountryDS.PrefetchPathToUse = prefetchPathRegion;
    }

}

Koen

koen75
User
Posts: 7
Joined: 21-Mar-2007
# Posted on: 21-Feb-2008 18:50:29   

....flushed

I used the field this._regionLocale instead of the property this.RegionLocale, when the field is null the property will contain the empty EntityCollection<RegionLocale>

Thank you! Koen