Changed records in subcollection are not reflected

Posts   
 
    
jonas
User
Posts: 16
Joined: 04-Oct-2005
# Posted on: 01-Nov-2005 11:18:48   

Hi

I have a StockEntity with a subcollection of StockTransactions. The first time I fetch the StockEntity everything works fine. But if one of the items in the subcollection are changed in the database, these changes are not reflected when I fetch again (I use the same instance of the StockEntity object)

Lets say that a value in the StockEntity table has been changed and another value has been changed in a related record in the StockTransaction table, then the StockEntity is updated correctly, but not the changed value in the StockTransaction object. What should I do to make the llblgen code reflect these changes too?

My code looks like follows:

IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.StockEntity); prefetchPath.Add(StockEntity.PrefetchPathStockTransaction);

// // Fetch data // adapter.FetchEntity(entity, prefetchPath);

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Nov-2005 15:03:17   

Normaly this should work fine!! I guess the problem may be outside those lines of code you send.

Are you using Context Objects?

jonas
User
Posts: 16
Joined: 04-Oct-2005
# Posted on: 01-Nov-2005 15:24:46   

No, we are not using ContextObjects.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Nov-2005 15:37:35   

I have to ask this one: Did you get the information (data is not updated) from the UI or by debugging? A possible cause can be that the new StockTransaction is not loaded into the UI in the second time for some reason

jonas
User
Posts: 16
Joined: 04-Oct-2005
# Posted on: 01-Nov-2005 18:24:46   

I assumed that the cause was in some other code, but I have run the following test. I prepared a nice word document for you with screenshots to assist, but it wasn't possible to upload documents, so I hope that this is enough. I mixed up a couple of tablesearlier, so I hope that you don't get too confused. The table that I am having trouble with is StockSum.

1 At first, i located relevant data using Query Analyzer. I had one record in the Stock table and two related records in the StockSum table.

2 The following code is used to fill a list with all StockEntities.


EntityCollection entities = new EntityCollection(new StockEntityFactory());

adapter.FetchEntityCollection(entities, null);

return entities;

3 The user doubleclicks on a row in a grid and the StockEntity item is passed on to the code below. All Subcollections are loadad.


try { DataAccessAdapter adapter = GetAdapter(); IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.StockEntity); prefetchPath.Add(StockEntity.PrefetchPathStockExtraValue); prefetchPath.Add(StockEntity.PrefetchPathStockTrans); prefetchPath.Add(StockEntity.PrefetchPathStockSum);

//
// Fetch data
//
adapter.FetchEntity(entity, prefetchPath);
System.Diagnostics.Debug.WriteLine("LoadStock for StockId" + entity.Id.ToString() + " returned " + entity.StockSum.Count.ToString() + " StockSumEntities");

return true;

} catch(Exception ex) { System.Diagnostics.Debug.Assert(false, ex.ToString()); }

return false;

4 I have a breakpoint at "return true" and added 4 watches. The data looks just like in the databasen

5 Next thing I do is that I change the values in the database. Name of the stock is changed, as well as the field Qty in the underlying StockSum table.

6 Once again the user doubleclicks a row and the code under section 3 is executed. Another stop at my breakpoint. I now see that the Name of the StockEntity has been updated. The underlying StockSumEntity has however not been updated. If i run the code under section 2, however (i.e. update the list) the updates are reflected

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 03-Nov-2005 11:50:43   

You re-fetch the data in the parent entity, and a new stocksum object is loaded into memory, though teh databound object is the old version.

Please alter section three as follows:


try
{
    DataAccessAdapter adapter = GetAdapter();
    IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.StockEntity);
    prefetchPath.Add(StockEntity.PrefetchPathStockExtraValue);
    prefetchPath.Add(StockEntity.PrefetchPathStockTrans);
    prefetchPath.Add(StockEntity.PrefetchPathStockSum);
    
    //
    // Fetch data
    //
    Context c = new Context();
    c.Add(entity);
    adapter.FetchEntity(entity, prefetchPath);
    System.Diagnostics.Debug.WriteLine("LoadStock for StockId" + entity.Id.ToString() + " returned " + entity.StockSum.Count.ToString() + " StockSumEntities");
    
    return true;
}
catch(Exception ex)
{
    System.Diagnostics.Debug.Assert(false, ex.ToString());
}

This will use a context object for the fetching which will make sure the new stocksum entity data is loaded into the old stocksum object. As databinding works with events fired, it might be you're still seeing the old version, even though the old version is updated.

Frans Bouma | Lead developer LLBLGen Pro
jonas
User
Posts: 16
Joined: 04-Oct-2005
# Posted on: 03-Nov-2005 21:37:18   

Thanks, everything seems to work fine now.

/Jonas