Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I navigate through multiple relationships using LINQ to Entities?
    primarykey
    data
    text
    <p>I'm just starting out with LINQ to Entities, and I'm trying to replicate a search we currently do in a stored procedure as a LINQ to Entities query. However, I can't seem to figure out the correct way to query properties of entities more than one "join" away from the starting point of my query.</p> <p>For example, say I have tables Campaign, CampaignLocation, and Location, which have foreign keys from Campaign to CampaignLocation, and CampaignLocation to Location. Pretty standard many-to-many table configuration. When I start writing my LINQ query like this:</p> <pre><code> var campaigns = from c in context.Campaign.CampaignLocation </code></pre> <p>that's as far in the association chain as I can go. There doesn't seem to be a "Location" property available on "CampaignLocation" so that I can filter on properties of the Location.</p> <p>I tried using the LINQ join syntax like this:</p> <pre><code> var campaigns = from c in context.Campaign join cl in context.CampaignLocation on c.CampaignID equals cl.CampaignID </code></pre> <p>But there doesn't seem to be a "CampaignID" property on the "cl" alias. Which is really weird, there's a column named that on the table. Is it not on the model object since it's the foreign key to the Campaign table?</p> <p>Where am I going wrong here, and what am I missing?</p> <p><strong>[UPDATE]</strong></p> <p>It looks like any integer columns I use for foreign keys are not being added as properties of the model objects. The foreign key relationships are displayed, but the ID properties aren't there. Is there any way to get the designer to add these properties to the model when I read the schema from the database?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload