Incorrect data retrieval?

Posts   
 
    
Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 24-Mar-2008 22:18:35   

I've run into a problem that is baffling me.

I am retrieving rows from a table (ActivityRelationship) and am finding that two int columns that I am interested in ("Activity1" and "Activity2") are being retrieved with the same values when they should be different!

See the attached screenshot that shows the table in SQL Server Management Studio along with the column values in Visual Studio. Visual Studio shows that the entity has the value 1208 for both "Activity1" and "Activity2" instead of 1211 and 1208, respectively.

Note that this table has a circular relationship to itself. I wonder if that is related to the problem.

In the attached file I've also included by llblgen project file and a definition of the table and its keys.

LLBLGen v2.5 Final ORMSupportClasses.NET20.dll file version 2.5.8.122 Using Adapter for .NET 2.0 SQL Server 2005 Express No inheritence hierarchies but, as noted above, circular relationship involved.

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 25-Mar-2008 01:05:19   

Can't see nothing wrong, could you please the fetching code?

Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 25-Mar-2008 01:22:27   

Here is the entire method call where an entity (ActivityEntity) is retrieved. It all works fine with the expception of pulling the related entities of type ActivityRelationshipEntity (see code beginning with the "// Now get all related initiatives..." comment:


/// <summary>
    /// Fetches the ActivityEntity and its categories.  
    /// </summary>
    /// <param name="Activity">ActivityEntity to fill.  Set the Id field in this entity before calling.</param>
    /// <param name="Categories">EntityCollection of CategoryEntity to fill</param>
    /// <param name="ShowHiddenRoles">If false, omits members who have only a hidden role or just the role if a member has both hidden and non-hidden roles.</param>
    static public void LoadActivity(ref ActivityEntity Activity, ref EntityCollection<CategoryEntity> Categories, Boolean ShowHiddenRoles)
    {
        using (DataAccessAdapter daa = new DataAccessAdapter())
        {
            if (!daa.FetchEntity(Activity))
            {
                throw new Exception("Fetching ActivityEntity failed.  Activity Id = " + Activity.Id);
            }

            // And get the related OrganizationEntity (Lead Organization) if there is one specified
            if (Activity.LeadOrganization != null)
            {
                Activity.Organization = (OrganizationEntity)daa.FetchNewEntity(new OrganizationEntityFactory(), Activity.GetRelationInfoOrganization());
            }

            // And get the related ActivityStatusEntity (Status of this initiative) if there is one specified
            if (Activity.Status != null)
            {
                Activity.ActivityStatus = (ActivityStatusEntity)daa.FetchNewEntity(new ActivityStatusEntityFactory(), Activity.GetRelationInfoActivityStatus());
            }

            // And get the related UrlEntity (collaboration site url) if there is one specified
            if (Activity.CollaborationUrl != null)
            {
                Activity.Url = (UrlEntity)daa.FetchNewEntity(new UrlEntityFactory(), Activity.GetRelationInfoUrl());
            }

            // And get the related DomainEntity (Domain) if there is one specified
            if (Activity.Domain != null)
            {
                Activity.Domain_ = (DomainEntity)daa.FetchNewEntity(new DomainEntityFactory(), Activity.GetRelationInfoDomain_());
            }

            // Load members now...
            // ... prefetch roles and persons and persons' top-level organization
            IPrefetchPath2 prefetchPathMembers = new PrefetchPath2((int)EntityType.ActivityMemberEntity);
            prefetchPathMembers.Add(ActivityMemberEntity.PrefetchPathActivityMemberRoleJoin).SubPath.Add( ActivityMemberRoleJoinEntity.PrefetchPathActivityRole);
            prefetchPathMembers.Add(ActivityMemberEntity.PrefetchPathPerson_).SubPath.Add(PersonEntity.PrefetchPathPersonOrganizationJoin ).SubPath.Add(PersonOrganizationJoinEntity.PrefetchPathOrganization_);

            IRelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(ActivityMemberFields.Activity == Activity.Id);

            // Filter out members who have only the Role that is to be hidden
            if (!ShowHiddenRoles)
            {
                filter.Relations.Add(ActivityMemberEntity.Relations.ActivityMemberRoleJoinEntityUsingMember);
                filter.PredicateExpression.AddWithAnd(ActivityMemberRoleJoinFields.Role != Int32.Parse(ConfigurationManager.AppSettings["RoleIdToHide"]));
            }

            try
            {
                daa.FetchEntityCollection(Activity.ActivityMember, filter, prefetchPathMembers);
            }
            catch (Exception ex)
            {
                throw new Exception("Fetching ActivityMembers collection failed.  Activity Id = " + Activity.Id, ex);
            }

            // Add a removedentitytracker for members
            Activity.ActivityMember.RemovedEntitiesTracker = new EntityCollection<ActivityMemberEntity>();

            // Add a removedentitytracker for the members' roles
            foreach (ActivityMemberEntity member in Activity.ActivityMember)
            {
                member.ActivityMemberRoleJoin.RemovedEntitiesTracker = new EntityCollection<ActivityMemberRoleJoinEntity>();
            }

            // Now get a list of all categories that apply to an activity...
            IRelationPredicateBucket filterCategories = new RelationPredicateBucket();
            IPredicateExpression filterCategoriesExpression1 = new PredicateExpression();
            filterCategoriesExpression1.Add(CategoryFields.UsedByActivity == true);
            if (Activity.Domain != null)
            {
                filterCategoriesExpression1.AddWithAnd(CategoryFields.Domain == Activity.Domain);
            }
            IPredicateExpression filterCategoriesExpression2 = new PredicateExpression();
            filterCategoriesExpression2.Add(CategoryFields.UsedByActivity == true);
            filterCategoriesExpression2.AddWithAnd(CategoryFields.Domain == DBNull.Value);

            filterCategories.PredicateExpression.Add(filterCategoriesExpression1).AddWithOr(filterCategoriesExpression2);

            // Get their cardinalities too
            PrefetchPath2 prefetch = new PrefetchPath2(EntityType.CategoryEntity);
            prefetch.Add(CategoryEntity.PrefetchPathCardinality_);

            // ...make sure to sort then by DisplayOrder
            SortExpression sortCategories = new SortExpression(CategoryFields.DisplayOrder | SortOperator.Ascending);

            try
            {
                daa.FetchEntityCollection(Categories, filterCategories, 0, sortCategories, prefetch);
            }
            catch (Exception ex)
            {
                throw new Exception("Fetching Categories collection failed for Activities", ex);
            }

            // Now get all related initiatives...
            IRelationPredicateBucket filterRelatedInitiatives = new RelationPredicateBucket();
            filterRelatedInitiatives.PredicateExpression.Add(ActivityRelationshipFields.Activity1 == Activity.Id);
            filterRelatedInitiatives.PredicateExpression.AddWithOr(ActivityRelationshipFields.Activity2 == Activity.Id);

            try
            {
                daa.FetchEntityCollection(Activity.ActivityRelationship, filterRelatedInitiatives);
            }
            catch (Exception ex)
            {
                throw new Exception("Fetching ActivityRelationship collection failed", ex);
            }
            // ...and loop through this list of related initiatives and fetch the relationship type
            // and activity information for each one.
            foreach (ActivityRelationshipEntity activityRelationship in Activity.ActivityRelationship)
            {
                activityRelationship.Activity_ = (ActivityEntity)daa.FetchNewEntity(new ActivityEntityFactory(), activityRelationship.GetRelationInfoActivity_());
                activityRelationship.ActivityRelationshipType = (ActivityRelationshipTypeEntity)daa.FetchNewEntity(new ActivityRelationshipTypeEntityFactory(), activityRelationship.GetRelationInfoActivityRelationshipType());

                // Get record in same table pointed to by Inverse field which represents the inverse relationship.  There should be 1 and 1 only.
                EntityCollection<ActivityRelationshipTypeEntity> inverseRelationshipTypes = new EntityCollection<ActivityRelationshipTypeEntity>();
                daa.FetchEntityCollection(inverseRelationshipTypes, activityRelationship.ActivityRelationshipType.GetRelationInfoActivityRelationshipType_());

                activityRelationship.ActivityRelationshipType.ActivityRelationshipType_.Add(inverseRelationshipTypes[0]);
            }

            // Add a removedentitytracker for related initiatives
            Activity.ActivityRelationship.RemovedEntitiesTracker = new EntityCollection<ActivityRelationshipEntity>();

            // And get all the activity's documents with their statuses
            IRelationPredicateBucket filterOutputs = new RelationPredicateBucket();
            filterOutputs.PredicateExpression.Add(OutputFields.Activity == Activity.Id);
            
            IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.OutputEntity);
            prefetchPath.Add(OutputEntity.PrefetchPathOutputStatus);

            try
            {
                daa.FetchEntityCollection(Activity.Output, filterOutputs, 0, null, prefetchPath);
            }
            catch (Exception ex)
            {
                throw new Exception("Fetching Output collection failed", ex);
            }

            // Add a removedentitytracker for documents
            Activity.Output.RemovedEntitiesTracker = new EntityCollection<OutputEntity>();
        }
    }

    static public void LoadOutput(ref OutputEntity Output, ref EntityCollection<CategoryEntity> Categories, ref EntityCollection<CategoryEntity> ActivityCategories)
    {
        LoadOutput(ref Output, ref Categories, ref ActivityCategories, true);
        return;
    }

    /// <summary>
    /// Fetches an OutputEntity, all it's related enties, its categories and the categories of its parent activity.
    /// </summary>
    /// <param name="Output"></param>
    /// <param name="Categories"></param>
    /// <param name="ActivityCategories"></param>
    /// <param name="OmitParentActivity"></param>
    static public void LoadOutput(ref OutputEntity Output, ref EntityCollection<CategoryEntity> Categories, ref EntityCollection<CategoryEntity> ActivityCategories, bool OmitParentActivity)
    {
        Int32? parentActivityDomain = null;

        using (DataAccessAdapter daa = new DataAccessAdapter())
        {
            if (!daa.FetchEntity(Output))
            {
                throw new Exception("Fetching OutputEntity failed.  Output Id = " + Output.Id);
            }

            // Get the related UrlEntity if there is one specified
            if (Output.Url != null)
            {
                Output.Url_ = (UrlEntity)daa.FetchNewEntity(new UrlEntityFactory(), Output.GetRelationInfoUrl_());
            }

            // And get the related OutputStatusEntity if there is one specified
            Output.OutputStatus = (OutputStatusEntity)daa.FetchNewEntity(new OutputStatusEntityFactory(), Output.GetRelationInfoOutputStatus());

            // And get the parent Activity if needed
            if (!OmitParentActivity)
            {
                ActivityEntity parentActivity = new ActivityEntity();
                parentActivity.Id = Output.Activity;
                LoadActivity(ref parentActivity, ref ActivityCategories);
                Output.Activity_ = parentActivity;
                parentActivityDomain = Output.Activity_.Domain;
            }
            else
            {
                ActivityEntity parentActivity = new ActivityEntity();
                parentActivity.Id = Output.Activity;
                if (!daa.FetchEntity(parentActivity))
                {
                    throw new Exception("Fetching parent ActivityEntity failed for Output Id = " + Output.Id);
                }
                parentActivityDomain = parentActivity.Domain;
            }

            // Now get a list of all categories that apply to the output...
            IRelationPredicateBucket filterCategories = new RelationPredicateBucket();
            IPredicateExpression filterCategoriesExpression1 = new PredicateExpression();
            filterCategoriesExpression1.Add(CategoryFields.UsedByOutput == true);
            if (parentActivityDomain != null)
            {
                filterCategoriesExpression1.AddWithAnd(CategoryFields.Domain == parentActivityDomain);
            }
            IPredicateExpression filterCategoriesExpression2 = new PredicateExpression();
            filterCategoriesExpression2.Add(CategoryFields.UsedByOutput == true);
            filterCategoriesExpression2.AddWithAnd(CategoryFields.Domain == DBNull.Value);

            filterCategories.PredicateExpression.Add(filterCategoriesExpression1).AddWithOr(filterCategoriesExpression2);

            // Get their cardinalities too
            PrefetchPath2 prefetch = new PrefetchPath2(EntityType.CategoryEntity);
            prefetch.Add(CategoryEntity.PrefetchPathCardinality_);

            // ...make sure to sort then by DisplayOrder
            SortExpression sortCategories = new SortExpression(CategoryFields.DisplayOrder | SortOperator.Ascending);

            try
            {
                daa.FetchEntityCollection(Categories, filterCategories, 0, sortCategories, prefetch);
            }
            catch (Exception ex)
            {
                throw new Exception("Fetching Categories collection failed for Output" + Output.Id, ex);
            }
        }
    }

    static public void InitNewActivityEntity(ref ActivityEntity NewActivity, ref EntityCollection<CategoryEntity> Categories, Int32 DomainId)
    {
        using (DataAccessAdapter daa = new DataAccessAdapter())
        {
            // And get the related DomainEntity (Domain) if there is one specified
            NewActivity.Domain = DomainId;

            NewActivity.Draft = true;
            NewActivity.Approved = false;

            // Fetch Domain
            NewActivity.Domain_ = (DomainEntity)daa.FetchNewEntity(new DomainEntityFactory(), NewActivity.GetRelationInfoDomain_());

            // Set up empty ActivityMember collection
            // NewActivity.ActivityMember = new EntityCollection<ActivityMemberEntity>();

            // Now get a list of all categories that apply to the domain
            IRelationPredicateBucket filterCategories = new RelationPredicateBucket();
            IPredicateExpression filterCategoriesExpression1 = new PredicateExpression();
            filterCategoriesExpression1.Add(CategoryFields.UsedByActivity == true);
            IPredicateExpression filterCategoriesExpression2 = new PredicateExpression();
            filterCategoriesExpression2.Add(CategoryFields.Domain == NewActivity.Domain);
            filterCategoriesExpression2.AddWithOr(new FieldCompareNullPredicate(CategoryFields.Domain, null));

            filterCategories.PredicateExpression.Add(filterCategoriesExpression1);
            filterCategories.PredicateExpression.AddWithAnd(filterCategoriesExpression2);

            // ...make sure to sort then by DisplayOrder
            SortExpression sortCategories = new SortExpression(CategoryFields.DisplayOrder | SortOperator.Ascending);

            try
            {
                daa.FetchEntityCollection(Categories, filterCategories, 0, sortCategories);
            }
            catch (Exception ex)
            {
                throw new Exception("Fetching Categories collection failed for Activity", ex);
            }


            // Set up empty related initiative collection
            // NewActivity.ActivityRelationship = new EntityCollection<ActivityRelationshipEntity>();

            // Set up empty list of documents
            // NewActivity.Output = new EntityCollection<OutputEntity>();
        }
    }


daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Mar-2008 05:37:16   

Mmm.. I can't see where is the matter. disappointed Could you please isolate the interested part (ActiveRelationship fetch) and see if everything works ok. Also make sure the DB data is the right one (I know... it's just to be sure).

David Elizondo | LLBLGen Support Team
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Mar-2008 06:38:42   
// Now get all related initiatives...
            IRelationPredicateBucket filterRelatedInitiatives = new RelationPredicateBucket();
            filterRelatedInitiatives.PredicateExpression.Add(ActivityRelationshipFields.Activity1 == Activity.Id);
            filterRelatedInitiatives.PredicateExpression.AddWithOr(ActivityRelationshipFields.Activity2 == Activity.Id);

            try
            {
                daa.FetchEntityCollection(Activity.ActivityRelationship, filterRelatedInitiatives);
            }

Do you notice that you are filling the _Activity.ActivityRelationship _collection? That collection is related to the _Activity _through _Activity1 _field. But the resulset is returning a record in which _Activity2 _is equal to 1208. So when you assing that resulset to the Activity.ActivityRelationship collection, the PK-FK sync will assign the 1208 value to the _Activity1 _field of the results (the field is the connection between ActiviyEntity and its ActivityRelationshipCollection).

I don't know what you are trying to achieve in that snippet but you should re-check what collections and references are you assigning.

David Elizondo | LLBLGen Support Team
Emmanuel
User
Posts: 167
Joined: 13-Jan-2006
# Posted on: 31-Mar-2008 23:41:41   

Thank you daelmo! You are correct - I was retrieving into the wrong collection!