views:

350

answers:

1

Hi guys,

I have the following schema.

What I'd like to do is to retrieve all customers (and children properties) from a selected StaffAssignment by StaffId.

So I've written the following query:

from c in ObjectContext.Customers.Include("Assignments.Activities")
               from a in c.Assignments
               from sa in a.StaffAssignments
               where sa.StaffId == staffId
               select c

But children properties aren't loaded (I added the [Include] in service metadata file as well).

What did I do wrong?

Gtz, Stéphane.

A: 

Hello Stéphane, I had the same issue.

This is my model (taken from NorthWind)

alt text

And the DomainService looks like:

[EnableClientAccess()]
public class DomainService1 : LinqToEntitiesDomainService<AdventureWorksLT2008Entities>
{
    public IQueryable<Customer> GetCustomer()
    {
        return this.ObjectContext.Customer.Include("CustomerAddress").Include("CustomerAddress.Address");
    }
}

I've added [Include] attributes on all navigation properties:

public partial class Customer
{
    internal sealed class CustomerMetadata
    {
        // Metadata classes are not meant to be instantiated.
        private CustomerMetadata() { }

        public string CompanyName { get; set; }

        [Include]
        public EntityCollection<CustomerAddress> CustomerAddress { get; set; }


And in the client code, I use this:

var dc = new MyDomainContext();

var query = dc.GetCustomerQuery();

var op = dc.Load(query, loadOperation =>
{
    var customer = loadOperation.Entities.FirstOrDefault();
    var address = customer.CustomerAddress.FirstOrDefault().Address;
}, null);


Note that this does only work when:

  • ObjectContext is auto generated by EntityFramework (no custom Context or Poco entities)
  • The DomainService is a 'LinqToEntitiesDomainService'

See also this link.

Stef