newbie question about entitystate

Posts   
 
    
slade52
User
Posts: 46
Joined: 15-Aug-2007
# Posted on: 25-Sep-2007 06:45:12   

Using SelfServicing.

I have a scenario using the tables "DebtorGroup" and "DebtorGroupCompany"

DebtorGroup has a one-to-zero/one relationship to DebtorGroupCompany. i.e. for a DebtorGroup row, there will be either one or zero DebtorGroupCompany rows.

What I need to know is, once I've fetched a DebtorGroupEntity, does it have a related DebtorGroupCompany record or not.

Will this code work OK ?

IPrefetchPath prefetchPath = new PrefetchPath((int)EntityType.DebtorGroupEntity);
prefetchPath.Add(DebtorGroupEntity.PrefetchPathDebtorGroupCompany);
DebtorGroupEntity dg = new DebtorGroupEntity(DebtorGroupUID, prefetchPath);

if (dg.Fields.State == EntityState.Fetched)
{

// code removed for brevity

if (dg.DebtorGroupCompany.Fields.State == EntityState.Fetched)
{

 // this code should only run when there is a valid related DebtorGroupCompany in the database

}
}

I guess my concern is whether dg.DebtorGroupCompany will be a null reference, or alternatively that by referencing it, it will create a new DebtorGroupCompany entity if one wasn't retrieved from the database.

Thanks in advance Mike

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 26-Sep-2007 11:28:46   

You could set the project property 'LazyLoadingWithoutResultsReturnsNew' to false, which means that if dg.DebtorGroupCompany isn't found, it will return null. (regenerate the code after the property change in the designer)

This is preferable in your scenario, because you can then do:

DebtorGroupCompanyEntity dgc = dg.DebtorGroupCompany; if(dgc==null) { // not found dgc = new DebtorGroupCompanyEntity(); dg.DebtorGroupCompany = dgc; } // work with dgc

Frans Bouma | Lead developer LLBLGen Pro
slade52
User
Posts: 46
Joined: 15-Aug-2007
# Posted on: 30-Sep-2007 23:51:47   

Thanks for your help Frans. Cheers Mike