title: Using a Lookup Table for Description field with Entity Framework Model First Approach
body: <p>I am currently redeveloping a site in MVC5 using EntityFramework6 and am struggling with the database structure in particular transforming the lookupFields from one table to the next. We have a Job Table</p>
  
  <pre><code>TABLE [dbo].[Job](
  [JobId] [int] IDENTITY(1,1) NOT NULL,
  [JobStateId] [int] NOT NULL,
  </code></pre>
  
  <p>and a JobState Lookup Table</p>
  
  <pre><code>TABLE [dbo].[JobState](
  [JobStateId] [int] NOT NULL,
  [Description] [varchar](50) NOT NULL
  </code></pre>
  
  <p>Now I am trying to use the description from the lookup table when viewing the JobView that has been generated using Entity Framework</p>
  
  <pre><code>public partial class Job
  {
      public int JobId { get; set; }
      public int JobStateId { get; set; }
      public int JobPriorityId { get; set; }
      public string JobType { get; set; }
      public Nullable&lt;System.DateTime&gt; RunAfter { get; set; }
      public int AttemptCount { get; set; }
      public int MaxAttemptCount { get; set; }
      public int RetryDelayMinutes { get; set; }
      public string ContentXml { get; set; }
      public Nullable&lt;int&gt; ContentId { get; set; }
      public System.DateTime Created { get; set; }
      public Nullable&lt;int&gt; SecondaryContentId { get; set; }
      public Nullable&lt;int&gt; ParentId { get; set; }
      public Nullable&lt;bool&gt; SingleChild { get; set; }
  }
  </code></pre>
  
  <p>I have created a partial view and added in the JobState Field</p>
  
  <pre><code> [MetadataType(typeof(JobMetadata))]
  public partial class Job
  {
      [ForeignKey("JobState")]
      public virtual JobState JobState { get; set; }
  }
  </code></pre>
  
  <p>and am trying to use this in the View</p>
  
  <pre><code> @foreach (var item in Model)
  {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.JobId)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.JobState)
          &lt;/td&gt;
  </code></pre>
  
  <p>however this does not produce a value.</p>
  
  <p>What would be the best way, with the least amount of code to achieve this?</p>
  
  <p>Should I generate enums from this lookup table and then keep them in sync, or create a relationship between these tables? I have looked into using this <a href="https://github.com/timabell/ef-enum-to-lookup/blob/master/ExampleUsage/EnumExample.cs" rel="nofollow">EF to enum package</a> however it requires migration scripts and the Seed() function which has not been created using the Database first approach. </p>
  
  <p>I have also tried creating a partial view for the JobStates</p>
  
  <pre><code>@inherits System.Web.Mvc.WebViewPage&lt;TS2.Models.Job&gt;
  @Html.DropDownListFor(m =&gt; m, new SelectList((IEnumerable&lt;TS2.Models.Job&gt;)ViewBag.JobStates,
  "JobStateId",
  "Description",
  Model == null ? 1 : Model.JobStateId),
  "Choose--")
  </code></pre>
  
  <p>and then using the editorFor approach in the view, but to no avail.</p>
  
  <p>Can anyone guide me in the right direction?</p>