title: Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework
body: <p>I am building an ASP.NET MVC 5 "To Do List" app, where users can add categories and add tasks to categories. I am using Entity Framework 7 with a sort of "Code First" approach, but using an existing database.</p>
  
  <p>I created a database in Microsoft SQL Server Management Studio with a <code>Category</code> and an <code>Item</code> table, and each <code>Item</code> has a <code>CategoryId</code> column that is a foreign key to the Category's <code>Id</code> column.</p>
  
  <p>Then I created an empty MVC5 app, with a database connection to this database. I created an <code>Item</code> class and a <code>Category</code> class in the <em>Models</em> folder that correspond to the tables in the database.</p>
  
  <p>Item class:</p>
  
  <pre><code>namespace ToDoList.Models
  {
      [Table("Item")]
      public partial class Item
      {
          public int ItemId { get; set; }
          public string Description { get; set; }
  
          [ForeignKey("Category")]
          public int CategoryId { get; set; }
  
          public virtual Category Category { get; set; }
      }
  }
  </code></pre>
  
  <p>Category class:</p>
  
  <pre><code>namespace ToDoList.Models
  {
      [Table("Category")]
      public partial class Category
      {
          public Category()
          {
              this.Items = new HashSet&lt;Item&gt;();
          }
  
          public int CategoryId { get; set; }
          public string Name { get; set; }
          public virtual ICollection&lt;Item&gt; Items { get; set; }
      }
  }
  </code></pre>
  
  <p>I want the view for an <code>Item</code> to display its <code>Category</code>'s <code>Name</code> property. The model for the view is a collection of all <code>Item</code>s in the <code>Item</code> table (<code>IEnumerable&lt;ToDoList.Models.Item&gt;</code>). Say I want to print a list with each item followed by the name of the category related to it. Here's the code that I have:</p>
  
  <pre><code>    &lt;ul&gt;
      @foreach(var item in Model)
      {
          &lt;li&gt;@Html.DisplayFor(modelItem =&gt; item.Description) @Html.DisplayFor(modelItem =&gt; item.Category.Name)&lt;/li&gt;;
  
      }
      &lt;/ul&gt;
  </code></pre>
  
  <p>The <code>Item</code> shows up just fine, but the page doesn't display anything for the <code>Category</code>. This same code actually worked when I used an ADO.NET Entity Data Model to create classes, but I'm not able to create an Entity Data Model using ASP.NET 5, so I'm trying out this code-first approach.</p>
  
  <p>Any ideas on why the app isn't using the foreign key relationship or suggestions of what I can try?</p>