Object Property Null After Setting ID

Posts   
 
    
SpykLee
User
Posts: 8
Joined: 06-Jul-2005
# Posted on: 06-Jul-2005 15:02:43   

Hi,

I've encountered a problem, using the July 4th ms sql 2000 version, Selfservicing;

Given the following example:

ConfigurationCollection configs = new ConfigurationCollection(); Context context = new Context(); context.Add(configs); configs.GetMulti(null);

ConfigurationEntity config = configs[0]; int oldStatusID = config.StatusTypeId; StatusTypeEntity status_1 = config.StatusType; // status_1 set

config.StatusTypeId = 2; StatusTypeEntity status_2 = config.StatusType; // status_2 set config.StatusTypeId = oldStatusID;

StatusTypeEntity status_3 = config.StatusType; // status_3 not set !? System.Diagnostics.Debug.Assert(status_3!=null);

Why is status_3 not set to the status_1 object?

(Configuration.StatusTypeID is a foreignkey to StatusType)

Cheers! Eric

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 07-Jul-2005 10:12:48   

SpykLee wrote:

Hi,

I've encountered a problem, using the July 4th ms sql 2000 version, Selfservicing;

Given the following example:


ConfigurationCollection configs = new ConfigurationCollection();
Context context = new Context();
context.Add(configs);
configs.GetMulti(null);

ConfigurationEntity config = configs[0];
int oldStatusID = config.StatusTypeId;
StatusTypeEntity status_1 = config.StatusType; // status_1 set

config.StatusTypeId = 2;
StatusTypeEntity status_2 = config.StatusType; // status_2 set
config.StatusTypeId = oldStatusID;

The last line in this blow, where you set the statustypeid, sets a new value in the FK field. this means that it has to dereference the object related using the OLD value: it sets the reference (config.StatusType) to null.


StatusTypeEntity status_3 = config.StatusType; // status_3 not set !?
System.Diagnostics.Debug.Assert(status_3!=null);

Why is status_3 not set to the status_1 object?

Because it can't necessarily know what the object is with the PK value oldStatusID. I can agree with the fact that the entity is added to a context and the context knows, so if it's in the context, it should make the reference... though it can also be the entity isn't in a context or the context doesn't contain the related entity, because it's not fetched (or will be added later).

Hence the manual action: don't set the FK field StatusTypeID, but set config.StatusType to status_1. This will set the FK field as well.

Frans Bouma | Lead developer LLBLGen Pro