How to get the types of related entities through an EntityRelation object?

Posts   
 
    
SanderF
User
Posts: 125
Joined: 11-Dec-2006
# Posted on: 28-Dec-2009 16:27:19   

Hi there,

We have a DocumentEntity related to an UserEntity.

Thus we can traverse the graph:

DocumenEntity document = new DocumentEntity();
document.Owner.LoginName; // Owner is of type UserEntity
document.Author.LoginName; // Author is of type UserEntity
document.CheckedOutBy.LoginName; // CheckedOutBy is of type UserEntity

Now we want all the related entities of a DocumentEntity which are of type UserEntity at runtime.

While iterating through all EntityRelation objects of the collection

DocumentEntity.Relations.GetAllRelations()

we cannot find a way to get all relations of a document which are of type UserEntity.

Do you have any suggestions?

Kind regards.

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 28-Dec-2009 19:46:02   

Hi Sander -

What you're really looking for is the EntityType from a Field. You need to use Reflection to do this. Here's what we use:


Type entityType = assembly.GetType(namespace + entityField.ContainingObjectName);

Where assembly & namespace are determined by any other Entity in the assembly. So, you will need one Entity (of any EntityType) to use Reflection. **entityField **is either a PrimaryKey or ForeignKey used in the Relation.

Realize, each Relation has two perspectives, what we've termed a RelationView:

  • PrimaryKey > ForeignKey
  • PrimaryKey < ForeignKey So, if you want to know the EntityType on the other end of the Relation - you will need to know which RelationView you are looking from. This can be determined by the StartEntityIsPkSide.

Hope this helps!

Ryan

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 28-Dec-2009 21:01:54   

You could also 'read' the ActualContainingObjectName property from a Relation's PK or FK field by use of the GetPKEntityFieldCore/GetFKEntityFieldCore property.

Please look at the following sample code where we inspect relations within a bucket to create a cloned version of the relations to create seamless access to an archive database :


    Private Function GetClonedRelationCollection(ByVal sourceRelations As IRelationCollection) As IRelationCollection

        If sourceRelations IsNot Nothing AndAlso sourceRelations.Count > 0 Then
            Dim result As New RelationCollection
            For Each relation As IEntityRelation In sourceRelations
                If archiveEntityList.Contains(relation.GetPKEntityFieldCore(0).ActualContainingObjectName) OrElse _
                    archiveEntityList.Contains(relation.GetFKEntityFieldCore(0).ActualContainingObjectName) Then
                    Dim newRelation As New EntityRelation(GetClonedEntityField(relation.GetPKEntityFieldCore(0)), _
                                                          GetClonedEntityField(relation.GetFKEntityFieldCore(0)), relation.TypeOfRelation, _
                                                          relation.StartEntityIsPkSide, relation.MappedFieldName)
                    result.Add(newRelation, relation.AliasStartEntity, relation.AliasEndEntity, relation.JoinType)
                Else
                    result.Add(relation, relation.AliasStartEntity, relation.AliasEndEntity, relation.JoinType)
                End If
            Next
            Return result
        End If
        Return Nothing
    End Function

Regards, Danny

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Dec-2009 06:23:35   

You also could do this:

static void DoSomethingWithUserEntities(IEntity2 entity)
{
    // get related entities
    List<IEntity2> relatedEntities = entity.GetDependentRelatedEntities();

    // loop into related entities
    foreach (IEntity2 tmpEntity in relatedEntities)
    {
        // *** the related entity is a UserEntity
        if (tmpEntity.LLBLGenProEntityTypeValue == (int)EntityType.UserEntity)
        {
            // do something with tmpEntity which is an UserEntity
        }
    }
}
David Elizondo | LLBLGen Support Team
SanderF
User
Posts: 125
Joined: 11-Dec-2006
# Posted on: 29-Dec-2009 16:08:03   

Hi,

Thanks for the posts.

I've solved it in another way by looking at the relations of the entity I do know and with the help of the method

document.GetRelationsForFieldOfType("Permissions")

Thanks anyway!

Regards