Entity updated from DB, odd behaviour

Posts   
 
    
LarsAO
User
Posts: 1
Joined: 24-Nov-2011
# Posted on: 24-Nov-2011 18:49:22   

Hi!

I've got a question about the behaviour described in the two examples below.

Setup LLBLGen PRo 3.1 Sql Server 2008

Table1 UniqueID (PK) Value

Table2 UniqueID (PK) Table1ID (FK) Value

Table1 contains row (1,"ABC")

Table2 contains rows (1, 1, "aaa") (2, 1, "bbb")

The following code:


Table1Entity t = new Table1Entity();
t.UniqueID = 1;
int count = t.Table2s.Count;

results in count = 2.

The following code:


Table1Entity t = new Table1Entity(1);
int countBefore = t.Table2s.Count;
t.UniqueID = 1;
int countAfter = t.Table2s.Count;

results in countBefore = 0 and countAfter = 0.

So, accessing the related entities in code snippet one loads the related entities from DB. By first accessing the related entities in code snippet two, I somehow break the expected behaviour. A 0 before is not strange since there is no primary key value to look up. But, the 0 after?

Now, is this by design, a bug or something I've done wrong when creating my model in LLBLGen Pro?

If it's by design, is there some explanation to this behaviour? If I've done something to cause this, I'd like some hints.

Let me know if you can't reproduce the behaviour.

Thanks in advance!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Nov-2011 21:50:30   

What seems odd to me is that you are saying that at the second line of the 2nd snippet there is no info to look at, but you are actually passing the PK:

Table1Entity t = new Table1Entity(1);
int countBefore = t.Table2s.Count;
t.UniqueID = 1;
int countAfter = t.Table2s.Count;

Now, if countBefore and countAfter is 0 this is expected. At line (2) you are actually fetching Table2 collection. Then at line (4) this is different because Table2 collection is already fetched, so it just returns the items already in memory.

If you want to force the fetch at line (4) you can tell "t" that it's not fetched already.

...
t.AlreadyFetchedTable2 = false;
int countAfter = t.Table2s.Count;

This is indeed the expected behavior in SelfServicing. Although I think you have something odd in your code. If yo still have problems please post your real code.

David Elizondo | LLBLGen Support Team