Note that there are some explanatory texts on larger screens.

plurals
  1. POExisting navigation properties missing
    primarykey
    data
    text
    <p>I'm currently in the process of checking out the possibilities of OData on an already existing Web Api project. Using Code First, I'm creating all models from scratch so that I have absolute control over them. However, even though I have added navigation properties in the code, some of them are missing in the schema when I'm checking it out through the metadata link OData provides.</p> <p>For example, I have a User class which inherits from a Person class:</p> <pre class="lang-cs prettyprint-override"><code>[DataContract] [KnownType(typeof(User))] public abstract class Person { [DataMember] public int ID { get; set; } [DataMember] public string Name { get; set; } [DataMember] public Nullable&lt;int&gt; CountryID { get; set; } [DataMember] public Nullable&lt;int&gt; RelationID { get; set; } public virtual Country Country { get; set; } public virtual Relation Relation { get; set; } } </code></pre> <p>Now when I do a GET request on this, using <code>/User(1284)/Relation</code> (or <code>/Country</code> for that matter) I get exactly the Relation or Country class back that I want. The problem is, however, that I cannot call <code>/Relation(16)/Country</code>, because this association is not present.</p> <pre class="lang-cs prettyprint-override"><code>[DataContract] public class Relation { [DataMember] public int ID { get; set; } [DataMember] public string Number { get; set; } [DataMember] public string Serial { get; set; } [DataMember] public Nullable&lt;int&gt; CountryID { get; set; } public virtual Country Country { get; set; } public virtual List&lt;User&gt; Users { get; set; } } </code></pre> <p>But as you can see, in my relation class, there definitely is such a navigation property present. Also, when you look at the DbContext class:</p> <pre class="lang-cs prettyprint-override"><code> public DbSet&lt;Relation&gt; Relations { get; set; } public DbSet&lt;Country&gt; Countries { get; set; } public DbSet&lt;User&gt; Users { get; set; } </code></pre> <p>and WebApiConfig </p> <pre class="lang-cs prettyprint-override"><code> ODataModelBuilder modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet&lt;Relation&gt;("Relations"); modelBuilder.EntitySet&lt;Country&gt;("Countries"); modelBuilder.EntitySet&lt;User&gt;("Users"); </code></pre> <p>You can see that actually relation and user are almost identical in terms of navigation properties, at least on Country they are identical.</p> <p>Only, as I said, when I look at the metadata provided by OData itself:</p> <pre class="lang-cs prettyprint-override"><code> &lt;Association Name="TestProject_Models_User_Country_TestProject_Models_Country_CountryPartner"&gt; &lt;End Type="TestProject.Models.Country" Role="Country" Multiplicity="0..1" /&gt; &lt;End Type="TestProject.Models.User" Role="CountryPartner" Multiplicity="0..1" /&gt; &lt;/Association&gt; &lt;Association Name="TestProject_Models_User_Relation_TestProject_Models_Relation_RelationPartner"&gt; &lt;End Type="TestProject.Models.Relation" Role="Relation" Multiplicity="0..1" /&gt; &lt;End Type="TestProject.Models.User" Role="RelationPartner" Multiplicity="0..1" /&gt; &lt;/Association&gt; </code></pre> <p>You can see that User->Country and User->Relation exists, but the associations of Relation to Country and User is missing. In the database, however, these relations do exists and the foreign keys are in place. When I run Update-Database in the NuGet console I also get the notification that there are no code updates to be done.</p> <p>I've already dropped my entire database and let CodeFirst recreate everything; updated to the latest stable build of OData (from 4.0.0 to 4.0.30506), but alas, nothing is working.</p> <p>Does anyone have any leads for me to follow? Thanks in advance!</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