Can DataAccessAdapter.UpdateEntitiesDirectly Update An Aliased Table?

Posts   
 
    
JPowell
User
Posts: 3
Joined: 19-Sep-2022
# Posted on: 19-Sep-2022 16:01:18   

I am using DataAccessAdapter.UpdateEntitiesDirectly by(pseudocode):

bucket = new RelationPredicateBucket({{PREDICATE}});
bucket.Relations.Add(ParentTableEntity.Relations.ChildTableEntityUsingParentTableId, "UPDATE_ALIAS", JoinHint.Inner);
bucket.Relations.Add(ParentTableEntity.Relations.ChildTableEntityUsingParentTableId, "JOIN_ALIAS", JoinHint.Inner);
bucket.SelectListAlias = "UPDATE_ALIAS";
var updateEntity  = new ChildTableEntity
{
    ChildField = "SOME_NEW_VALUE"
};
using var adapter = new DataAccessAdapter();
adapter.UpdateEntitiesDirectly(updateEntity, bucket);

The generated sql is(pseudocode):

UPDATE ChildTable
SET {{VARIOUS_FIELDS}}
FROM ParentTable INNER JOIN ChildTable [UpdateAlias] ON {{CONDITIONS}}
                 INNER JOIN ChildTable [JoinAlias] ON {{CONDITIONS}}
WHERE {{CONDITIONS}}

But what I need is(pseudocode):

UPDATE [UpdateAlias]
SET {{VARIOUS_FIELDS}}
FROM ParentTable INNER JOIN ChildTable [UpdateAlias] ON {{CONDITIONS}}
                 INNER JOIN ChildTable [JoinAlias] ON {{CONDITIONS}}
WHERE {{CONDITIONS}}

Thank you very much for any assistance!

Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 19-Sep-2022 22:14:28   

In your example you can try keeping the Join_Alias, and disregard the Update_Alias. You only need to alias the Child table once if it's joined twice.

JPowell
User
Posts: 3
Joined: 19-Sep-2022
# Posted on: 20-Sep-2022 16:54:32   

Thank you Walaa, I have tested your suggestion and have run into the following:

---> System.NullReferenceException: Object reference not set to an instance of an object.
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityRelation.DetermineAlias(Boolean pkSide)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityRelation.get_AliasFKSide()
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityRelation.GetUsedEntityTypeNamesAndAliases(MultiValueHashtable`2 entityNamesPerAlias, Dictionary`2 artificialAliasPerEntity)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityRelation.SD.LLBLGen.Pro.ORMSupportClasses.IRelation.GetUsedEntityTypeNamesAndAliases(MultiValueHashtable`2 entityNamesPerAlias, Dictionary`2 artificialAliasPerEntity)
   at SD.LLBLGen.Pro.ORMSupportClasses.RelationCollection.GetUsedEntityTypeNamesAndAliases(MultiValueHashtable`2 entityNamesPerAlias, Dictionary`2 artificialAliasPerEntity)
   at SD.LLBLGen.Pro.ORMSupportClasses.PersistenceCore.AddAdditionalInheritanceRelationsToRelationCollectionBasedOnFilter(IRelationCollection relations, IPredicateExpression filter, IInheritanceInfoProvider infoProvider)
   at SD.LLBLGen.Pro.ORMSupportClasses.Adapter.QueryCreationManager.CreateQueryForUpdateEntitiesDirectly(IEntity2 entityWithNewValues, IRelationPredicateBucket filterBucket)
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterCore.UpdateEntitiesDirectlyAsync(IEntity2 entityWithNewValues, IRelationPredicateBucket filterBucket, CancellationToken cancellationToken)

I am guessing this is because the UPDATE_ALIAS was referenced(before I removed it per your suggestion) in other Relations(which I did not enumerate) and also in the {{PREDICATE}}?

JPowell
User
Posts: 3
Joined: 19-Sep-2022
# Posted on: 20-Sep-2022 20:18:54   

UPDATE: The above issue has been resolved. The issue was due to changing from:

bucket.Relations.Add(ParentTableEntity.Relations.ChildTableEntityUsingParentTableId, "UPDATE_ALIAS", JoinHint.Inner);

To:

bucket.Relations.Add(ParentTableEntity.Relations.ChildTableEntityUsingParentTableId, null, JoinHint.Inner);

Specifying a null end entity Alias caused the null pointer. I changed the code to:

bucket.Relations.Add(ParentTableEntity.Relations.ChildTableEntityUsingParentTableId);

And it appears to be working.