RIA Services query: Nested "from" and include children
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.I开发者_高级运维nclude("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.
I had the same issue.
This is my model (taken from NorthWind)
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.
精彩评论