title: OData and WebAPI: Navigation property not present on model
body: <p>I'm trying to put together a simple toy project using Entity Framework, WebAPI, OData, and an Angular client. Everything is working fine, except the navigation property that I have put on one of my models doesn't seem to be working. When I call my API using $expand, the returned entities do not have their navigation properties.</p>
  
  <p>My classes are Dog and Owner, and look like this: </p>
  
  <pre><code>    public class Dog
  {
      // Properties
      [Key]
      public Guid Id { get; set; }
      public String Name { get; set; }
      [Required]
      public DogBreed Breed { get; set; }
      public int Age { get; set; }
      public int Weight { get; set; }
  
  
      // Foreign Keys
      [ForeignKey("Owner")]
      public Guid OwnerId { get; set; }
  
      // Navigation
      public virtual Owner Owner { get; set; }
  }
  
      public class Owner
  {
      // Properties
      public Guid Id { get; set; }
      public string Name { get; set; }
      public string Address { get; set; }
      public string Phone { get; set; }
      public DateTime SignupDate { get; set; }
  
      // Navigation
      public virtual ICollection&lt;Dog&gt; Dogs { get; set; } 
  }
  </code></pre>
  
  <p>I also have my Dog controller set up to handle querying:</p>
  
  <pre><code>public class DogsController : ODataController
  {
      DogHotelAPIContext db = new DogHotelAPIContext();
      #region Public methods 
  
      [Queryable(AllowedQueryOptions = System.Web.Http.OData.Query.AllowedQueryOptions.All)]
      public IQueryable&lt;Dog&gt; Get()
      {
          var result =  db.Dogs.AsQueryable();
          return result;
      }
  
      [Queryable(AllowedQueryOptions = System.Web.Http.OData.Query.AllowedQueryOptions.All)]
      public SingleResult&lt;Dog&gt; Get([FromODataUri] Guid key)
      {
          IQueryable&lt;Dog&gt; result = db.Dogs.Where(d =&gt; d.Id == key).AsQueryable().Include("Owner");
          return SingleResult.Create(result);
      }
  
      protected override void Dispose(bool disposing)
      {
          db.Dispose();
          base.Dispose(disposing);
      }
  
  }
  </code></pre>
  
  <p>I've seeded the database with a bit of sample data. All dog records have an OwnerId that matches the Id of an Owner in the Owners table.</p>
  
  <p>Querying for the list of dogs using this works fine:</p>
  
  <pre><code>http://localhost:49382/odata/Dogs
  </code></pre>
  
  <p>I get a list of Dog entities, without the Owner navigation property.</p>
  
  <p>Querying for the dogs with their owners using OData $expand does NOT work:</p>
  
  <pre><code>http://localhost:49382/odata/Dogs?$expand=Owner
  </code></pre>
  
  <p>My response is a 200 with all of the Dog entities, but none of them have an Owner property on them in the JSON.</p>
  
  <p>If I query my metadata, I find that OData does seem to know about it:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx"&gt;
    &lt;edmx:DataServices&gt;
      &lt;Schema Namespace="DogHotelAPI.Models" xmlns="http://docs.oasis-open.org/odata/ns/edm"&gt;
        &lt;EntityType Name="Dog"&gt;
          &lt;Key&gt;
            &lt;PropertyRef Name="id" /&gt;
          &lt;/Key&gt;
          &lt;Property Name="id" Type="Edm.Guid" Nullable="false" /&gt;
          &lt;Property Name="name" Type="Edm.String" /&gt;
          &lt;Property Name="breed" Type="DogHotelAPI.Models.Enums.DogBreed" Nullable="false" /&gt;
          &lt;Property Name="age" Type="Edm.Int32" Nullable="false" /&gt;
          &lt;Property Name="weight" Type="Edm.Int32" Nullable="false" /&gt;
          &lt;Property Name="ownerId" Type="Edm.Guid" /&gt;
          &lt;NavigationProperty Name="owner" Type="DogHotelAPI.Models.Owner"&gt;
            &lt;ReferentialConstraint Property="ownerId" ReferencedProperty="id" /&gt;
          &lt;/NavigationProperty&gt;
        &lt;/EntityType&gt;
        &lt;EntityType Name="Owner"&gt;
          &lt;Key&gt;
            &lt;PropertyRef Name="id" /&gt;
          &lt;/Key&gt;
          &lt;Property Name="id" Type="Edm.Guid" Nullable="false" /&gt;
          &lt;Property Name="name" Type="Edm.String" /&gt;
          &lt;Property Name="address" Type="Edm.String" /&gt;
          &lt;Property Name="phone" Type="Edm.String" /&gt;
          &lt;Property Name="signupDate" Type="Edm.DateTimeOffset" Nullable="false" /&gt;
          &lt;NavigationProperty Name="dogs" Type="Collection(DogHotelAPI.Models.Dog)" /&gt;
        &lt;/EntityType&gt;
      &lt;/Schema&gt;
      &lt;Schema Namespace="DogHotelAPI.Models.Enums" xmlns="http://docs.oasis-open.org/odata/ns/edm"&gt;
        &lt;EnumType Name="DogBreed"&gt;
          &lt;Member Name="AfghanHound" Value="0" /&gt;
          &lt;Member Name="AmericanStaffordshireTerrier" Value="1" /&gt;
          &lt;Member Name="Boxer" Value="2" /&gt;
          &lt;Member Name="Chihuahua" Value="3" /&gt;
          &lt;Member Name="Dachsund" Value="4" /&gt;
          &lt;Member Name="GermanShepherd" Value="5" /&gt;
          &lt;Member Name="GoldenRetriever" Value="6" /&gt;
          &lt;Member Name="Greyhound" Value="7" /&gt;
          &lt;Member Name="ItalianGreyhound" Value="8" /&gt;
          &lt;Member Name="Labrador" Value="9" /&gt;
          &lt;Member Name="Pomeranian" Value="10" /&gt;
          &lt;Member Name="Poodle" Value="11" /&gt;
          &lt;Member Name="ToyPoodle" Value="12" /&gt;
          &lt;Member Name="ShihTzu" Value="13" /&gt;
          &lt;Member Name="YorkshireTerrier" Value="14" /&gt;
        &lt;/EnumType&gt;
      &lt;/Schema&gt;
      &lt;Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm"&gt;
        &lt;EntityContainer Name="Container"&gt;
          &lt;EntitySet Name="Dogs" EntityType="DogHotelAPI.Models.Dog"&gt;
            &lt;NavigationPropertyBinding Path="owner" Target="Owners" /&gt;
          &lt;/EntitySet&gt;
          &lt;EntitySet Name="Owners" EntityType="DogHotelAPI.Models.Owner"&gt;
            &lt;NavigationPropertyBinding Path="dogs" Target="Dogs" /&gt;
          &lt;/EntitySet&gt;
        &lt;/EntityContainer&gt;
      &lt;/Schema&gt;
    &lt;/edmx:DataServices&gt;
  &lt;/edmx:Edmx&gt;
  </code></pre>
  
  <p>What could I be missing that is preventing my navigation preoprty from coming back with the rest of my model?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>To further isolate the problem I tried including the Owners in C# on the server side. I added this line in the Get method of my Dog controller:</p>
  
  <pre><code>var test = db.Dogs.Include("Owner").ToList();
  </code></pre>
  
  <p>With this I can debug and see that the related owners ARE being included. Each dog has the owner that is associated with it in this list.</p>
  
  <p>Using .Include("Owner") on what is actually returned does not fix the problem - the properties still never reach the client.</p>
  
  <p>This seems to mean that the navigation properties are working, but are not being sent back to the client. This seems like it wound indicate an issue with OData or WebAPI, I would guess, but I'm not sure what.</p>
  
  <p>Also, I have added the following lines to Application_Start in my Global.asax file in order to handle circular navigation properties:</p>
  
  <pre><code>            var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
          json.SerializerSettings.PreserveReferencesHandling =
              Newtonsoft.Json.PreserveReferencesHandling.All;
  </code></pre>
  
  <p>I did that in case a circular reference was somehow the culprit, but this changes nothing.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I noticed that making a call to </p>
  
  <pre><code>http://localhost:49382/odata/Dogs(abfd26a5-14d8-4b14-adbe-0a0c0ef392a7)/owner
  </code></pre>
  
  <p>works. This retrieves the owner associated with that dog. This further illustrates that my navigation properties are set up correctly, they just <strong>aren't being included in responses to calls using $expand.</strong></p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Here is the register method of my WebApiConfig file:</p>
  
  <pre><code>        public static void Register(HttpConfiguration config)
      {
          //config.Routes.MapHttpRoute(
          //    name: "DefaultApi",
          //    routeTemplate: "api/{controller}/{id}",
          //    defaults: new { id = RouteParameter.Optional }
          //);
  
          ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
          builder.EnableLowerCamelCase();
          builder.EntitySet&lt;Dog&gt;("Dogs");
          builder.EntitySet&lt;Owner&gt;("Owners");
  
          config.EnableQuerySupport();
  
          config.MapODataServiceRoute(
              routeName: "ODataRoute",
              routePrefix: "odata",
              model: builder.GetEdmModel());
  
  
          // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable&lt;T&gt; return type.
          // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
          // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
          //config.EnableQuerySupport();
  
          // To disable tracing in your application, please comment out or remove the following line of code
          // For more information, refer to: http://www.asp.net/web-api
          config.EnableSystemDiagnosticsTracing();
      }
  </code></pre>