0

I am trying to build a query in LINQ for EF5. My model has set up an Association with two foreign keys, shippingcompanycontacts with a contactid and a shippingcompanyid. There is no intellisense for that "table" (it shows up as a table in mySQL) I tried accessing the data this way to no avail. ( I hard coded the id)

 Dim testquery = (From s In ctx.shippingcompanies Where s.Id = 1 Join
                c In ctx.contacts On s.Id Equals c.Id Select New With {c.LastName}).ToList()

I really need to access the association table but there is no intellisense for it

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
dinotom
  • 4,990
  • 16
  • 71
  • 139

1 Answers1

2

The association table is not available in EF by default. You have only Contacts and ShippingCompanies and each of them contains navigation property to their related counter part so your query can be defined as (C#):

var query = from s in ctx.shippingcompanies 
            where s.Id == 1
            from c in s.contacts // accessing navigation property
            select c.LastName;

You can manually edit EDMX and bring ShippingCompanyContacts as a new entity but it is not usual way to use EF.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Can you explain why s.contacts works? Isn't s just the shippingcompanies table in variable form? It has no field contacts, so why does s.contacts work? (Which it does by the way) – dinotom Jan 04 '13 at 11:19
  • That is the magic behind EF. It translates the navigation property back into relations in database and makes joins with junction table and contacts table. – Ladislav Mrnka Jan 04 '13 at 11:23