Fabien Warniez
Fabien Warniez

Reputation: 2741

With WCF Data Services, is it possible to use the $expand command on joint tables

I am trying to do something fairly easy to understand with WCF data services, but can't find how to do it.

I have 3 table, Customer, Product and a joint table Customer_Product linking the two other tables (a basic n to n relationship): Customer <= Customer_Product => Product

I want to get a customer and its products in the same query, so I would like to do something like:

/Service.svc/Customers(23)?$expand=Products

But it tells me that there is no Products navigation property on the table Customer.

The only option that I found is to do:

/Service.svc/Customers(23)?$expand=Customer_Product

and then make another call to get the Product details.

Is there a clean way to do this?

Thanks a lot in advance.

Upvotes: 0

Views: 744

Answers (1)

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13320

The many to many relationships are usually modeled by the service by hiding the join table (if the only thing it stores is the relationship and there's no data in it). If you're using EF in the service this should be pretty easy to do. If you do need to expose the join table for some reason, then you can issue a query like:

/Service.svc/Customers(23)?$expand=Customer_Product/Product

(expands can be multiple levels deep). Of course reading the results will be a bit more complicated because of the two levels there, but you do get the data you need.

Upvotes: 1

Related Questions