ORMEntityOutOfSyncException - Refilling Entity With ExcludeIncludeFieldsList

Posts   
 
    
Posts: 15
Joined: 13-Mar-2011
# Posted on: 19-May-2011 04:18:34   

Hi there,

I attempted to replicate the example code in the documentation topic "Generated code - Excluding / Including fields for fetches, Adapter".

My code is:


        public EntityCollection<IllustrationEntity> GetIllustrationsExcludingDiagram(out IllustrationEntity refilledEntity)
        {
            ExcludeIncludeFieldsList excludedFields = new ExcludeIncludeFieldsList();
            excludedFields.Add(IllustrationFields.Diagram);

            EntityCollection<IllustrationEntity> illlustrations = new EntityCollection<IllustrationEntity>();
            SortExpression sorter = new SortExpression(IllustrationFields.IllustrationId | SortOperator.Descending);

            using (DataAccessAdapter productsAdapter = DataAccess.AdaptorFactory.GetNewAdaptor(true))
            {
                productsAdapter.FetchEntityCollection(illlustrations, null, 0, sorter, null, excludedFields);
            }

            //  Now, re-hydrate one of the entities. 
            refilledEntity = new IllustrationEntity(1);
            using (DataAccessAdapter productsAdapter = DataAccess.AdaptorFactory.GetNewAdaptor(true))
            {
                productsAdapter.FetchEntity(refilledEntity, null, null, excludedFields);
            }

            using (DataAccessAdapter productsAdapter = DataAccess.AdaptorFactory.GetNewAdaptor(true))
            {
                productsAdapter.FetchExcludedFields(illlustrations, excludedFields);
                productsAdapter.FetchExcludedFields(refilledEntity, excludedFields);
            }
            
            return illlustrations;
        }

An exception is thrown: ORMEntityOutOfSyncException The entity is out of sync with its data in the database. Refetch this entity before using this in-memory instance.

Can anyone please explain where I have gone wrong. Using the AdventureWorks database.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-May-2011 07:44:24   

Are you sure the Illustration with IllustrationId=1 already exists in DB? As a matter of fact, the common AdventureWorks DB installation script doesn't come with an Illustration with PK 1. So, as the record doesn't exists in DB, LLBLGen will leave the refilledEntity as new and in a OutOfSync state: the entity is your code is not the same as the record in DB (it doesn't exist). Then the error occurs because you are trying to re-hydrate an entity that is treated as new (it didn't fetched successfully).

So, to avoid this, modify your code this way:

...

// Now, re-hydrate one of the entities. 
            refilledEntity = new IllustrationEntity(1);
            using (DataAccessAdapter productsAdapter = DataAccess.AdaptorFactory.GetNewAdaptor(true))
            {
                productsAdapter.FetchEntity(refilledEntity, null, null, excludedFields);
            }

            using (DataAccessAdapter productsAdapter = DataAccess.AdaptorFactory.GetNewAdaptor(true))
            {
                productsAdapter.FetchExcludedFields(illlustrations, excludedFields);
            
                if (!refilledEntity.IsNew)
                {
                     productsAdapter.FetchExcludedFields(refilledEntity, excludedFields);
                }
            }
            
            return illlustrations;
David Elizondo | LLBLGen Support Team
Posts: 15
Joined: 13-Mar-2011
# Posted on: 24-May-2011 08:22:28   

Thanks daelmo. You are correct - there is no pk 1 in the Illustration relation (I'm a dunce).

That's a great tip about checking the IsNew property. Thanks very much for that!