Adapter - FetchEntity -> How to know if an Entity does not exist?

Posts   
 
    
rodri_ogri
User
Posts: 22
Joined: 05-Nov-2010
# Posted on: 19-Nov-2010 00:15:56   

Hi,

I guess this is a well know Issue, but I couldn't figure it out,

I'm making a call to the FetchEntity method with its corresponding entity to retrieve. It has a primaryKey loaded but how could I know if that entity really exists on the DB? After making the retrieval, my entity's properties have this value:

"'.[Field]' threw an exception of type 'SD.LLBLGen.Pro.ORMSupportClasses.ORMEntityOutOfSyncException'"

Can you tell me the way to do it right?

Thanks a lot

Rodrigo

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Nov-2010 06:19:49   
David Elizondo | LLBLGen Support Team
rodri_ogri
User
Posts: 22
Joined: 05-Nov-2010
# Posted on: 19-Nov-2010 14:48:36   

Thanks a lot for your help simple_smile , but it doesn't work as I expect...

I'm encapsulating the fetching behavior passing the entity with the Id loaded, then after that I'm using it outside. After the fetch, I validate the boolean fetch result. If it's false, I set the entity as null, as follows:


// TEntity is of type IEntity2
public void FillEntity(TEntity pEntity)
        {
            bool vIsEntityFetched = false;
            // Adapter instance
            using (DataAccessAdapterBase vAdapter = GetAdapter())
            {
                // Gets the entity
                vIsEntityFetched = vAdapter.FetchEntity(pEntity);
                if (!vIsEntityFetched)
                    pEntity= null;
            }
        } 

I'm trying to follow the FetchEntity method behavior. I mean that I wouldn't like to return the entity, instead, I'd like to manipulate the one I have passed to my FillEntity method. BUT, after I set the entity to null, this one is still fetched with exceptions on the properties outside the method.

Isn't it supposed to remain as I modified it to null? but it's acting like a value type.

Am I right?

regards

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Nov-2010 18:25:47   

There is some things I want to revisit with you:

  1. Please try the suggestion posted here as I think something weird is in your code. Please regenerate code in an empty folder an create a tiny sample solution with similar scenario. It should work.

  2. The boolean result of adapter.FetchEntity represents the following:

  3. true: the Fetch was successful (whether or not the row exists in DB). So you can receive true and still the entity is IsNew=true.
  4. false: the Fetch was not successful (whether or not the row exists in DB). If you receive false maybe there is a problem with the query, maybe there is an authorization restriction.

  5. So, after (1) and (2). My recommendation is that you check the IsNew property of the entity. If after fetch IsNew=false, then the row exists on DB, if IsNew=true, the row doesn't exits in DB so the entity automatically is set as new.

David Elizondo | LLBLGen Support Team
rodri_ogri
User
Posts: 22
Joined: 05-Nov-2010
# Posted on: 19-Nov-2010 21:32:25   

Thanks a lot, I really appreciate your help,

I will figure the entity out based on that property,

regards

veljkoz
User
Posts: 25
Joined: 31-Mar-2010
# Posted on: 30-Jul-2012 15:49:05   

I'll just add another case where we had the problem with this, for all other guys out there.

We have an abstract BaseEntity and two entities that inherit from it - Entity1 and Entity2.

We used Id of Entity1, but the prefetch path was (wrongly) pointing to Entity2 type. Now when we tried adapter.FetchEntity() we did get a result because the base entity exists with that Id and some properties are fetched, but the the IsNew is set to 'true', and we got this unexpected exception on **some **of the properties:

SD.LLBLGen.Pro.ORMSupportClasses.ORMEntityOutOfSyncException: The entity is out of sync with its data in the database. Refetch this entity before using this in-memory instance.

Just had to figure out the wrong prefetch path start, and it was resolved.

btw, thanks for the great support wink