title: Foreign Key Entity Framework & Razor
body: <p>I am using Entity Framework 6, MVC 5 and the latest ASP.NET Razor.  </p>
  
  <p>I have the following code :</p>
  
  <pre><code>[ForeignKey("Country")]
  [Display(Name = "CountryId", ResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
  public int CountryId { get; set; }
  
  [Required(ErrorMessageResourceName = "Country_ValidationError", ErrorMessageResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
  [Display(Name = "Country", ResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
  public virtual Country Country { get; set; }
  
  [ForeignKey("Currency")]
  [Display(Name = "CurrencyId", ResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
  public int CurrencyId { get; set; }
  
  [Required(ErrorMessageResourceName = "Currency_ValidationError", ErrorMessageResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
  [Display(Name = "Currency", ResourceType = typeof(Internationalization.Resources.TenantFinanceConfiguration))]
  public virtual Currency Currency { get; set; }
  </code></pre>
  
  <p>Irrespective of where I place the foreign key, the tables are created correctly; but when I create the views:</p>
  
  <pre><code>&lt;div class="form-group"&gt;
    @Html.LabelFor(model =&gt; model.CountryId, "CountryId", htmlAttributes: new { @class = "control-label col-md-2" })
    &lt;div class="col-md-10"&gt;
      @Html.DropDownList("CountryId", null, htmlAttributes: new { @class = "form-control" })
      @Html.ValidationMessageFor(model =&gt; model.CountryId, "", new { @class = "text-danger" })
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
    @Html.LabelFor(model =&gt; model.CurrencyId, "CurrencyId", htmlAttributes: new { @class = "control-label col-md-2" })
    &lt;div class="col-md-10"&gt;
      @Html.DropDownList("CurrencyId", null, htmlAttributes: new { @class = "form-control" })
      @Html.ValidationMessageFor(model =&gt; model.CurrencyId, "", new { @class = "text-danger" })
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I get the error : </p>
  
  <blockquote>
    <p>The ViewData item that has the key 'CountryId' is of type 'System.Int32' but must be of type 'IEnumerable'.</p>
  </blockquote>
  
  <p>What I am missing, surely this should work as I do not require IEnumerable for a one-to-one relationship?</p>
  
  <p>Any assistance greatly appreciated.</p>