locked
RIA - Related Related Entities RRS feed

  • Question

  • Ok, so I know how to mark related entities with the <Include()> decorator in the metadata class and then use the Include("RelatedEntity") method in the load query.

    But what if the RelatedEntity also has RelatedEntities of it's own? How do I include those as well?

    For example, I have Client entities, Matter entities and MatterRates entities. Clients have Matters and Matters have MatterRates. In my client domain service metadata class, I have Matters defined as a complex property. No problem. In my Client domain service I include matters in my load query. But now, I need to also include the MatterRates. How do I do this? Since MatterRates are not a property of a client, it seems I would not include them in either the metadata class, nor in the Client load query, so I am at a loss as to where or how to do this.

    Thanks,
    Josh

    Thursday, March 31, 2011 1:50 PM

Answers

  • Well, I eventually figured this out for myself, so I figured I'd post the answer here.

    First, you have to generate the Domain Class and Metadata class for the related entity. In the metadata class for the related entity, mark it's related entities with the <include()> decorator.

    Then on the top-level class, you can string an additional include call that references the related-realted entity like this (following my example of Clients->Matters->MatterRates):

    Public Function GetClients(OfficeID As Guid) As IQueryable(Of Clients)
            Return Me.ObjectContext.Clients.Include("Matters").Include("Matters.MatterRates").Where(Function(o) o.OfficeID = OfficeID)
        End Function


    Hope this helps someone else who runs into the same issue. :)

    ~Josh

    Thursday, March 31, 2011 3:21 PM

All replies

  • Well, I eventually figured this out for myself, so I figured I'd post the answer here.

    First, you have to generate the Domain Class and Metadata class for the related entity. In the metadata class for the related entity, mark it's related entities with the <include()> decorator.

    Then on the top-level class, you can string an additional include call that references the related-realted entity like this (following my example of Clients->Matters->MatterRates):

    Public Function GetClients(OfficeID As Guid) As IQueryable(Of Clients)
            Return Me.ObjectContext.Clients.Include("Matters").Include("Matters.MatterRates").Where(Function(o) o.OfficeID = OfficeID)
        End Function


    Hope this helps someone else who runs into the same issue. :)

    ~Josh

    Thursday, March 31, 2011 3:21 PM
  • You don't need both includes in the query, Matters.MatterRates will also include Matters. A caution here, including across a Many relationship can have performance problems database side. Depending on the database side and the numbers of records returned be sure you monitor that. In general, .Include("A.B.C.D.E.F.Gs") is fine but .Include("A.Bs.C") is not.

     

    If the reason for the performance problem isn't obvious, try writing out a single query yourself that will be returning Clients, Matters, and MatterRates all in one query.

    Thursday, March 31, 2011 4:27 PM
  • Thanks Colin, you're the man!

    Friday, April 1, 2011 6:32 PM