title: How to determine if a PropertyType is foreign key
body: <p>I have the following class 'schakeling', generated with EF, representing a database table 'schakeling'. In the database 'id' is the primary key and 'plc_id' is a foreign key.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public partial class schakeling
  {
      public schakeling()
      {
          this.verbruik = new HashSet&lt;verbruik&gt;();
      }
  
      public int id { get; set; }
      public int plc_id { get; set; }
      public string var_output { get; set; }
      public string omschrijving { get; set; }
      public int vermogen { get; set; }
      public Nullable&lt;bool&gt; status { get; set; }
      public Nullable&lt;byte&gt; hourOn { get; set; }
      public Nullable&lt;byte&gt; minuteOn { get; set; }
      public Nullable&lt;byte&gt; hourOff { get; set; }
      public Nullable&lt;byte&gt; minuteOff { get; set; }
  
      public virtual plc plc { get; set; }
      public virtual ICollection&lt;verbruik&gt; verbruik { get; set; }
  }
  </code></pre>
  
  <p>I have a view class</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class SchakelingsListViewModel
  {
      public IEnumerable&lt;schakeling&gt; List { get; set; }
      public PagingInfo PagingInfo { get; set; }//Not relevant for this question
      //And some more properties...also not relevant
  }
  </code></pre>
  
  <p>I have the following view (omitted some HTML for brevity)</p>
  
  <pre class="lang-cs prettyprint-override"><code>@model PortalControl.ViewModels.SchakelingsListViewModel
  &lt;h2&gt;Index&lt;/h2&gt;
  &lt;table&gt;
  @foreach (var item in Model.List) {
      @Html.TableRowFor(item)
  }
  &lt;/table&gt;
  </code></pre>
  
  <p>I have a generic html helper method TableRowFor because I want to be able to use the method on other domain entities generated with EF. The method generates simple table data. </p>
  
  <pre class="lang-cs prettyprint-override"><code>public static MvcHtmlString TableRowFor&lt;T&gt;(this HtmlHelper helper, T obj)
  {
      string controller = obj.GetType().BaseType.Name;
      string id = obj.GetType().GetProperty("id").GetValue(obj).ToString();
  
      StringBuilder sb = new StringBuilder("&lt;tr&gt;");
      sb.Append("&lt;td&gt;");
      sb.Append("&lt;a href='" + controller + "/Edit/" + id + "'&gt;&lt;img src='/Images/edit-icon.png' /&gt;&lt;/a&gt;");
      sb.Append("&lt;a href='" + controller + "/Details/" + id + "'&gt;&lt;img src='/Images/details-icon.png' /&gt;&lt;/a&gt;");
      sb.Append("&lt;a href='" + controller + "/Delete/" + id + "'&gt;&lt;img src='/Images/delete-icon.png' /&gt;&lt;/a&gt;");
      sb.Append("&lt;/td&gt;");
  
      foreach (var property in obj.GetType().GetProperties())
      {
          //If statement below filters out the two virtual properties(plc, verbruik) of the schakeling class(as said, generated with EF), somewhat ugly but it works, better suggestions are welcome..
          if ((!property.PropertyType.Name.ToLower().Contains("icollection")) &amp;&amp; (property.PropertyType.CustomAttributes.Count() != 0))
          {
              sb.Append("&lt;td&gt;");
              //if property is foreign key
                  //sb.Append("&lt;a href='" + correctControllerNameHere + "/Details/" + property.GetValue(obj) + "'&gt;&lt;img src='/Images/details-icon.png' /&gt;&lt;/a&gt;");
              //else
                  //sb.Append(property.GetValue(obj));
              sb.Append("&lt;/td&gt;");
          }
      }
      sb.Append("&lt;/tr&gt;");
      return new MvcHtmlString(sb.ToString());
  }
  </code></pre>
  
  <p><b>The question I have is, I would like to create a link if a property is a foreign key.</b></p>
  
  <p>I have searched the internet but I am no expert in PropertyInfo, MetaDataClassType and other similar classes. Something like property.isForeign() would be lovely but anything that works will be appreciated.</p>