Should objects that were prefetched be reset on subsequent fetches?

Posts   
 
    
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 05-Feb-2005 12:13:19   

I have a scenario where I am performing a single entity fetch with a prefetch statement added to the fetch. I am filtering on the child entities.

Here is my test:

                PortalsEntity portal = new PortalsEntity(0);
                UsersEntity user = new UsersEntity();

                user.Username = "jbloggs";
                user.Password = "password";
                CheckForExistingUser(portal, user);

                user.Username = "xxx1234";
                user.Password = "0001234";

                //portal = new PortalsEntity(0);    
            
                CheckForNonExistingUser(portal, user);

Here are the results for the test above:

 First Count: 1
jbloggs
Second Count: 1
jbloggs
NUnit.Framework.AssertionException: Count of users should be 0?

Portal has users via an intermediate table PortalUser.

if I uncomment //portal = new PortalsEntity(0); then portal.Users.Count == 0 for the "CheckForNonExistingUser" method. These are the reslts when //portal = new PortalsEntity(0) is uncommented:


First Count: 1
jbloggs
Second Count: 0

Since the 2 check methods are executing the same query but with different values in the where clause, shouldnt the portal.Users collection be reset each time we call adapter.FetchEntity? When looking at the TSQL generated by the fetch, the first case executes 3 queries and gets 3 results. In the second case, the same 3 queries are executed (but with different values in the where clause) but only 1 result is returned.

Here is the fetch entity code:

        public PortalsEntity FindUserInPortal(PortalsEntity portal, UsersEntity user)
        {
            IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.PortalsEntity);

            IRelationPredicateBucket bucket = new RelationPredicateBucket();
            bucket.PredicateExpression.Add(PredicateFactory.CompareValue(
                UsersFieldIndex.Username, ComparisonOperator.Equal, user.Username));
            bucket.PredicateExpression.AddWithAnd(PredicateFactory.CompareValue(
                UsersFieldIndex.Password, ComparisonOperator.Equal, user.Password));

            prefetchPath.Add(PortalsEntity.PrefetchPathUsers,0, bucket.PredicateExpression);


            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntity(portal, prefetchPath);

                return portal;
            }

        }
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 05-Feb-2005 13:19:43   

By design, an entity collection where entities are fetched into is not cleared beforehand, to allow the developer to fetch data on the fly into a collection.

If you want to clear a collection before a fetch, override OnFetchEntityCollection in a derived class of DataAccessAdapter and clear the passed in entity collection there.

Frans Bouma | Lead developer LLBLGen Pro