0

I'm using EF 4.1 with Self-Tracking Entities in a layered application which uses WCF to hand entities back and forth from the client to the server.

A portion of my database contains 3 tables:

Customer
Contact
CustomerContacts

CustomerContacts contains only the two primary keys of Customer and Contact, thus the EDM represents this as navigation properties - Customer.Contacts and Contacts.Customers. The CustomerContacts table is not represented otherwise in the model, meaning there is no CustomerContacts entity, which I understand and expect as a feature of the EDM Designer when representing many-to-many relationships of this form.

The situation is that I have a list of Customers bound to a ComboBox and wish to load the related Contacts of a Customer only at the point when it is selected in the ComboBox. Put another way, I wish to explicilty load Customer.Contacts when a Customer is selected in the ComboBox. I can't use Customer.ID in a Where to fetch the list of Contacts, since there is no join entity in the model which relates them.

Currently, I am loading another copy of the Customer, using Include("Contacts") to get the Contacts and then setting via selectedCustomer.Contacts = temporaryCustomer.Contacts;

Does anyone know of another method which doesn't require that I fetch a redundant, temporary copy of the Customer?

Erikest
  • 4,997
  • 2
  • 25
  • 37

1 Answers1

1

You already know Id of selected customer so you can simply pass it to WCF as parameter and query contacts related to customer:

var customerContacts = context.Contacts
                              .Where(c => c.Customers.Any(cu => cu.Id == passedId));

Anyway you should think about replacing your WCF with STEs with WCF Data Services which are more suitable for this scenarios and support many features (including loading of navigation property) out-of-the-box.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • I looked at WCF Data Services, unfortunately after too much work had been done on this architecture. I liked what I saw, though wasn't sure where the BLL would fit in without having another service layer sit on top of the odata, effectively rendering the WCF service moot, since I would still have to expose its functionality through service contracts. Also, the examples I saw integrating asp.net membership, which I customize and use as my user/groups/roles provider, were trivial and/or tedious to implement, though I do see a path using queryinterceptors to get the fine grain control I require – Erikest Jul 09 '12 at 19:42
  • Your example though looks like it will work for me. I was struggling with a way to filter by Customer.ID when there was no link table in the model. I like this approach, using the navigation property within the where statement. I'll mark your answer after I verify that it works as expected :) Thanks! – Erikest Jul 09 '12 at 19:46
  • Well, I modified it to use Entity SQL, but the principle was the same and it worked, so thanks again :) – Erikest Jul 10 '12 at 00:17