Entity Framework 7 Lambda expression for 3 table joins
<p>I am trying to built an API that will return all the data for a server. A server can have multiple endpoints and they are stored in separate table with many-to-many relationship.</p>

<p>I have 3 tables:</p>

<ul>
<li><code>Servers</code> table contains all the server details</li>
<li><code>Applications</code> table that contains application names</li>
<li><code>application_endpoint</code> table that contains foreign keys to both tables <code>Servers</code> and <code>Applications</code></li>
</ul>

<p>Here is my database model:</p>

<p><a href="https://i.stack.imgur.com/r9hgD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r9hgD.png" alt="enter image description here"></a></p>

<p>Here are my models:</p>

<pre><code>public class Servers
{
        public int id { get; set; }
        public string server_name { get; set; }
        public string alias { get; set; }
        public string ip_address { get; set; }
        public Status status { get; set; }
        public virtual ICollection&lt;Application_endpoints&gt; endpoint { get; set; }
}

public class Application_endpoints
{
        public int id { get; set; }
        public int? server_id { get; set; }
        [ForeignKey("server_id")]
        public Servers server { get; set; }
        public int? application_id { get; set; }
        [ForeignKey("application_id")]
        public Applications application { get; set; }
}

public class Applications
{
        public int id { get; set; }
        public string app_name { get; set; }
}

public class ServerDbContext : DbContext
{
        public DbSet&lt;Applications&gt; Applications { get; set; }
        public DbSet&lt;Application_endpoints&gt; Application_endpoints { get; set; }
        public DbSet&lt;Servers&gt; Servers { get; set; }
}
</code></pre>

<p>In my Api Controller, I have created <code>HttpGet</code> method that will query database and return data for each server. Here is simple API for GET:</p>

<pre><code>private ServerDbContext _context;

public ServersController (ServerDbContext context)
{
    _context = context;
}

// GET: api/values
[HttpGet]
public JsonResult Get()
{
    var servers = _context.Servers
        .Include(endpoint =&gt; endpoint.endpoint)
        .ToList();
    return Json(servers);
}
</code></pre>

<p>Now when I make a get request, I get data back from the database, however the <code>application</code> object in below JSON is returned null. I am trying to figure it out how to add left join in my above query so I can get application name from applications table.</p>

<pre><code>{
  "id": 6,
  "server_name": "server1",
  "alias": "",
  "ip_address": "192.168.1.7",
  "endpoint": [
    {
      "id": 23,
      "server_id": 6,
      "application_id": 10,
      "application": null
    }
  ]
}
</code></pre>

<p>Any help is really appreciated. :)</p>