Relation Foreign Key Field Name

Posts   
 
    
Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 10-May-2005 17:23:15   

Ok... I give up... flushed I've been searching this forum and staring at the docs and code for a couple of hours now trying to figure this out.

In Template Studio, using the C# templates, how can I get the name of the related field from a given EntityRelation.

<%  
foreach(EntityRelation rel in Entity.Relations)
{
    if(rel.RelationType == EntityRelationType.OneToMany && !rel.UtilizingPropertyIsHidden)
    {
        if (Entity.PrimaryKeyFields.Count == 1) {
            string primaryKeyName = ((EntityFieldDefinition)Entity.PrimaryKeyFields[0]).FieldName;
            string foreignKeyName = ((EntityFieldDefinition)Entity.PrimaryKeyFields[0]).MappedField.FieldName;
%>

in the above code: primaryKeyName and foreignKeyName are always the same... (the PK) Am I missing something?

I need to name of the FK field in the related table... wink

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39786
Joined: 17-Aug-2003
# Posted on: 10-May-2005 17:44:49   

string fieldName = ((IDBField)((EntityFieldDefinition)Entity.PrimaryKeyFields[0]).MappedField).FieldName;

MappedField is a 'IEntityFieldMapTargetElement', which derives from IDBField. IDBField contains a property FieldName simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 10-May-2005 18:02:30   

Otis wrote:


string fieldName = ((IDBField)((EntityFieldDefinition)Entity.PrimaryKeyFields[0]).MappedField).FieldName;

MappedField is a 'IEntityFieldMapTargetElement', which derives from IDBField. IDBField contains a property FieldName simple_smile

wink yes that's what I had:

 string foreignKeyName = ((EntityFieldDefinition)Entity.PrimaryKeyFields[0]).MappedField.FieldName;

But the resulting value in foreignKeyName was always the same as primaryKeyName in the code above...

The primary key should have been "FolderUID" and the foreign key field should have been "ChildFolderUID" (as defined in LLBLGen Designer)... But both are "FolderUID".

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 10-May-2005 18:29:32   

Doooooh!!! flushed

Been looking at this for too long... I didn't even notice your code was different to mine!

But same problem cry ... I'm not sure that IDBField cast is required.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39786
Joined: 17-Aug-2003
# Posted on: 10-May-2005 19:24:01   

Ohhh!! I misunderstood you, I thought you couldn't retrieve the name from the field!.

I'll try to rewrite the code you're trying to achieve. (you're now grabbing the PK field, which obviously isn't correct, if you wnt to read the FK field)

Frans Bouma | Lead developer LLBLGen Pro
Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 10-May-2005 19:35:08   

Otis wrote:

Ohhh!! I misunderstood you, I thought you couldn't retrieve the name from the field!.

I'll try to rewrite the code you're trying to achieve. (you're now grabbing the PK field, which obviously isn't correct, if you wnt to read the FK field)

Thanks... I couldn't see anything in the EntityRelation that would help me either. disappointed I'm sure its something simple.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39786
Joined: 17-Aug-2003
# Posted on: 10-May-2005 20:16:12   

Marcus wrote:

Otis wrote:

Ohhh!! I misunderstood you, I thought you couldn't retrieve the name from the field!.

I'll try to rewrite the code you're trying to achieve. (you're now grabbing the PK field, which obviously isn't correct, if you wnt to read the FK field)

Thanks... I couldn't see anything in the EntityRelation that would help me either. disappointed I'm sure its something simple.

Yes, you have to use the EntityRelation.FieldRelations collection. That collection contains EntityFieldRelation objects: one per PK field (with its corresponding FK field).

So your code should be something like:


foreach(EntityRelation rel in Entity.Relations)
{
    if(rel.RelationType == EntityRelationType.OneToMany && !rel.UtilizingPropertyIsHidden)
    {
        if (Entity.PrimaryKeyFields.Count == 1) {
            // 1:n, so start entity is PK side.
            string primaryKeyName = rel.FieldRelations[0].RelationStartField.FieldName;
            string foreignKeyName = rel.FIeldRelations[0].RelationEndField.FieldName;

Frans Bouma | Lead developer LLBLGen Pro
Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 10-May-2005 23:20:58   

That's it... thanks!

The SDK documentation for "MappedField" states "The related field for this entity field" and I thought that meant "related" as in Foreign Key simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39786
Joined: 17-Aug-2003
# Posted on: 11-May-2005 10:10:32   

Marcus wrote:

That's it... thanks!

The SDK documentation for "MappedField" states "The related field for this entity field" and I thought that meant "related" as in Foreign Key simple_smile

Heh simple_smile Yeah, that's not very well formulated wink . I'll change it into 'mapped' simple_smile

Frans Bouma | Lead developer LLBLGen Pro
lyndon_h
User
Posts: 79
Joined: 14-Sep-2004
# Posted on: 12-Aug-2005 21:10:23   

Frans, I'm having a problem implementing the code you have provided above. My code is pasted below. I'm getting the infamous "cannot use foreach......object doesn't implement getEnumerator..." error. Do you know what is wrong? Thanks


            foreach(EntityRelation rel in EN_CUST_SQ_BANKEntity.Relations)
            {
                if(rel.RelationType == EntityRelationType.OneToMany && !rel.UtilizingPropertyIsHidden)
                {
                    if (sqBank.PrimaryKeyFields.Count == 1) 
                    {
                        // 1:n, so start entity is PK side.
                        string primaryKeyName = rel.FieldRelations[0].RelationStartField.FieldName;
                        string foreignKeyName = rel.FIeldRelations[0].RelationEndField.FieldName;

                        System.Web.HttpContext.Current.Response.Write("primary = " + primaryKeyName);
                        System.Web.HttpContext.Current.Response.Write("  foriegn = " + foreignKeyName);
                    }
                    System.Web.HttpContext.Current.Response.End();
                }
            }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39786
Joined: 17-Aug-2003
# Posted on: 13-Aug-2005 10:57:09   

Are you using the code in the snippet you posted to traverse the .lgp project object graph or the generated code? THe generated code uses a different object model.

Frans Bouma | Lead developer LLBLGen Pro
lyndon_h
User
Posts: 79
Joined: 14-Sep-2004
# Posted on: 14-Aug-2005 00:05:30   

I was using it for the generated code. I'll take a look at the documentation for the generated code. Thanks again Otis.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39786
Joined: 17-Aug-2003
# Posted on: 14-Aug-2005 10:55:44   

in that case: foreach(EntityRelation rel in (RelationCollection)EN_CUST_SQ_BANKEntity.Relations) { //... }

as the .Relations property returns an IRelationCollection type, which isn't enumerable, the RelationCollection is. simple_smile

Frans Bouma | Lead developer LLBLGen Pro