title: How to link tables in Entity Framework
body: <p>I am trying to create an asset tracking system using MVC and Entity Framework. The database already exists. I have a ComputerInventory table with a field of Location which is a forgein key to the the LocationID of the Location table. What im trying to do is when i output the information about the inventory, i dont have to see the LocationID, I want to see the Location field of the corrisponding LocationID.</p>
  
  <p>I have been using some tutproials but I can't seem to change it to suite my needs. I would think that this is pretty common so maybe i'm missing something. Here is my ComputerInventory:</p>
  
  <pre><code>[Table("ComputerInventory")]
  public class ComputerInventory
  {
      [Key]
      public String AssetTag {get; set;}
      //public Int32 Location { get; set; }
  
      [ForeignKey("Location")]
      public virtual Location Location { get; set; }
  
      public Int32 PoolType { get; set; }
      public Int32 Reason { get; set; }
      public Int32 InventoryStatus { get; set; }
      public Int32 InventoryType { get; set; }
      [StringLength(500)]
      public String Comments { get; set; }
      //public String Clock { get; set; }
  
     // public ICollection&lt;Location&gt; Locations { get; set; }
  }
  
  public class AssetDBContext : DbContext
  {
      public DbSet&lt;ComputerInventory&gt; ComputerInventory { get; set; }
  
      public AssetDBContext()
          : base("Name=LaptopLoaner_RW")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
          modelBuilder.Entity&lt;ComputerInventory&gt;().HasRequired(l =&gt; l.Location).WithMany();
      }
  }
  </code></pre>
  
  <p>Here is my Location model</p>
  
  <pre><code>[Table("Location")]
      public class Location
      {
          [Key]
          public Int32 Id { get; set; }
          //[Column(LocationName="LocationName")]
          [MaxLength(100)]
          public String LocationName { get; set; }
          [MaxLength(25)]
          public String Country { get; set; }
      }
  
      public class LocationDBContext : DbContext
      {
          public DbSet&lt;Location&gt; Locations { get; set; }
  
          public LocationDBContext()
              : base("Name=&lt;name&gt;")
          {
          }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
          }
      }
  </code></pre>
  
  <p>Here is the where I am trying to output the data in the view:</p>
  
  <pre><code>@model IEnumerable&lt;LaptopLoaner.Models.ComputerInventory&gt;
  @foreach (var item in Model) {
  &lt;tr&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.AssetTag)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.InventoryStatus)
      &lt;/td&gt;
      &lt;td&gt;
        @Html.DisplayFor(modelItem =&gt; item.Location.LocationName)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.PoolType)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Reason)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.InventoryType)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Comments)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.ActionLink("Edit", "Edit", new { id=item.AssetTag }) |
          @Html.ActionLink("Details", "Details", new { id=item.AssetTag }) |
          @Html.ActionLink("Delete", "Delete", new { id=item.AssetTag })
      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>}</p>
  
  <p>Do I have to change my ComputerInventory model or do I have to access the Location from the view?</p>