Foreign key column showing ID (need to navigate more than one entity to get to name) RRS feed

  • Question

  • User-1336852631 posted

    For my dynamic data application using Domain Service and Entity Framework, I have a model that includes the following entities:

    Client -> ClientProducts -> ReleaseHistory

    The ReleaseHistory page shows the foreign key for the ClientProducts table as a number (the id).   Normally to include the readable name for a foreign key, I use .Include() in the DomainService class, e.g. 

            public IQueryable<ClientReleaseHistory> GetClientReleaseHistories()
                    return ((ObjectQuery<ClientReleaseHistory>)(this.ObjectContext.ClientReleaseHistories.OrderBy(c => c.ID))).Include("ClientProduct").Include("ClientProduct.Product");

    This works fine where there is only one item in the .Include() call, but where I need to traverse more than one entity and have a dot separated list in .Include, it doesn't work (just displays the id).

    Any ideas on how I would show the Product Name instead of the foreign key ID in this case. I thought of adding code to the GridView RowDataBound event, but I don't know of a way to access the GridView's controls for DynamicData. The other option would be to use a custom field template, but I wondered if there was an easier way.

    Tuesday, June 26, 2012 1:20 PM


  • User-1336852631 posted

    Sorry to answer my own question!

    I realised I can solve this by adding a public property to the public partial class in the DomainService.metadata:

        public partial class ClientReleaseHistory
            public string Product
                    return this.ClientProduct.Product.ProductName;

    This then causes an additional Product column to appear showing the ProductName from the Product entity.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 27, 2012 8:00 AM