Getting fields from a related table

Posts   
 
    
Posts: 2
Joined: 26-Jul-2005
# Posted on: 26-Jul-2005 10:41:41   

I'm working on templates for our aspx and codebehind pages. Most tables we use contain a field Vch[tablename]Name, and as default I generate a column for this field in the datagrid.

The problem is that some tables do not contain a name field, and in this case the column should not be generated.

What I want to do is check if the related table contains a name field. How can I get the fields from a related Entity?

This is what I do so far:


<% foreach(EntityFieldDefinition field in Entity.Fields) {
       if(field.MappedField.IsForeignKey) {%>
//code to generate
<%}}%>

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39786
Joined: 17-Aug-2003
# Posted on: 26-Jul-2005 11:13:17   

You should traverse the Entity.Relations collection, then filter out any m:n relations by using the entityrelation.RelationType property. All relations in entity.Relations have 'entity' as start entity and the related entity as End entity. Then do: EntityFieldDefinition tablenamefield = relatedEntity.Fields["Vch..."]; if(tablenamefield!=null) { // is present, generate code }

Frans Bouma | Lead developer LLBLGen Pro
Posts: 2
Joined: 26-Jul-2005
# Posted on: 26-Jul-2005 12:08:17   

Works great, thanks a lot! smile