ORMEntityOutOfSyncException again

Posts   
 
    
JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 02-Mar-2006 17:59:14   

I understand why once an entity has been saved, it needs to be refetched. But I have another case where I get this exception. I've changed my code slightly to work around it, but I'm curious why it happens.

All my data hangs off a root entity (CompletionEntity). Now when the user opens a certain form, I want to fetch a related entity (BHAEntity) and its graph from the db, and of course if it isn't there, just create a new one. Here's my code from the BLL object:


        public BHAEntity GetBHA()
        {
            if (completion.BHA == null)
            {
                // note here CompletionEntity -> BHAEntity is 1-1
                BHAEntity bha = new BHAEntity(completion.CompletionID);
                if (completion.IsNew == false)
                {
                    // try to fetch from db
                    IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.BHAEntity);
                    // OMITTED: rest of prefetch logic
                    DataAccessAdapter adapter = new DataAccessAdapter();
                    // I thought if this fails, I don't care, I can still return bha
                    // but in fact when I bind a control to it I get ORMEntityOutOfSyncException
                    adapter.FetchEntity(bha, prefetchPath);

                    // my workaround:
                    if (!adapter.FetchEntity(bha, prefetchPath))
                        bha = new BHAEntity(completion.CompletionID);
                }

                completion.BHA = bha;
            }
            return completion.BHA;
        }





Ah....how nice it is not to see the spaces in my code swallowed!!!! And death to the tab character (but not the tab key). sunglasses

Jim

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 02-Mar-2006 18:47:18   

Reading data from an uninitialized entity gives this error. Do you read a property from the entity somewhere without first setting the value of that property?

Frans Bouma | Lead developer LLBLGen Pro
JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 02-Mar-2006 19:15:22   

When I first bind a control to a field, that triggers the error:


uiSumpPackerVendor.DataBindings.Add("EditValue", bha, "SumpPackerVendorID");

The thing is, when I implement my workaround, which is nothing more than instantiating the bha entity again, it works fine. It's doing a failed fetch on the new entity that causes problems. But if the fetch fails, why is the entity considered out of synch at that point?

Maybe it's just a philosophical way of looking at things? The entity represents the results of a failed fetch, and so is tainted? In my mind, if the fetch failed, the entity is untouched, and it is as if I never attempted the fetch.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Mar-2006 07:47:05   

OutOfSyncException, is the way to tell you that your entity might be out of Sync with the database, its values might be invalid now. (as least that's how I think of it)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 03-Mar-2006 08:34:07   

JimFoye wrote:

When I first bind a control to a field, that triggers the error:


uiSumpPackerVendor.DataBindings.Add("EditValue", bha, "SumpPackerVendorID");

The thing is, when I implement my workaround, which is nothing more than instantiating the bha entity again, it works fine. It's doing a failed fetch on the new entity that causes problems. But if the fetch fails, why is the entity considered out of synch at that point?

Maybe it's just a philosophical way of looking at things? The entity represents the results of a failed fetch, and so is tainted? In my mind, if the fetch failed, the entity is untouched, and it is as if I never attempted the fetch.

An entity is the data it represents. An entity object is just a container for that data. So an entity which failed to be fetched is nonexistend. You may have a bucket for it, the entity object, but that doesnt' mean the entity is there. So if you read from an entity object and it's out of sync, it means that the entity inside it is out of sync with itself and if there's no entity inside the entity object, that also means it's out of sync with itself, as it wasn't loaded properly or initialized.

If you really REALLY need to work around this, you can set the entity.fields.State to new or fetched, though if you set it to fetched, you suggest the entity is there while it isn't.

Frans Bouma | Lead developer LLBLGen Pro
JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 03-Mar-2006 17:09:00   

Otis wrote:

An entity is the data it represents. An entity object is just a container for that data. So an entity which failed to be fetched is nonexistend. You may have a bucket for it, the entity object, but that doesnt' mean the entity is there. So if you read from an entity object and it's out of sync, it means that the entity inside it is out of sync with itself and if there's no entity inside the entity object, that also means it's out of sync with itself, as it wasn't loaded properly or initialized.

Ok, that explains your position nicely. I guess I was thinking too literally about having an instance of a class (my entity class) which didn't get filled with database data, so ought to be resusable. I think I got burned by this before, under slightly different circumstances. But I'm "down with it', as they say. sunglasses