object.Refetch not refetching everything ?

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 28-May-2005 01:37:56   

Hi,

i have the following issue.

I have an entity with a n:m relation to another entity, while the linking object is also a real entity with datafields (To be specific, a Reception is m:n linked to a Perceel via a ReceptionPerceel, and a ReceptionPerceel has eg. a Zaaidatum field) . The linking object is shown in a datagrid,and i provide the user with a "delete" button to remove such an object from the collection. Stupid me, i didn't realize backthen that a collection is only a memory representation, and a removal from the collection doesnt actually delete the object from the DB. But it does update the databound grid correctly, the object is not in it anymore.

After some adding/removing the user can save the main entity, and the following buttonhandler is called:


        private void btnOpslaan_Click(object sender, System.EventArgs e)
        {
            PersistData();
            m_currentEntity.Refetch();
            SetBindings();
            this.Refresh();
        }

Everything looked ok in the grid after the save action, so i assumed it was all done correctly, after all, i do a refetch, re-set my bindings, and even do a refresh to make sure. I noticed a bit later on in the database that the m:n relations weren't deleted at all, which i understand now, i solved it, but why didn't the Refetch() show me that ?? (they should re-appear in the grid after the save) I changed my code then to :


        private void btnOpslaan_Click(object sender, System.EventArgs e)
        {
            PersistData();
            m_currentEntity = new ReceptionEntity(m_currentEntity.RecId);//instead of .Refetch()
            SetBindings();
            this.Refresh();
        }

and then indeed the grid shows me that the linkobjects aren't deleted at all.

Now my question : what's the difference between using entity.refetch() and really refetching it yourself - using entity = new Entity(exisiting_id) ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-May-2005 11:55:04   

Refetch fetches the fields. A new entity does that too through the PK fetch, but has one advantage: the lazy loading flags are false. This means that if a collection is already fetched in an existing entity, and you call Refetch on that entity, the collections already fetched aren't reset, nor are their lazy load flags: they stay fetched.

To switch off lazy loading for a collection, simply set entity.AlwaysFetch<fieldMappedOnRelation> to true. This will make each read of <FieldMappedOnRelation> (for example customer.Orders) to refetch the data from the database, till you set it to false again of course wink

Frans Bouma | Lead developer LLBLGen Pro