title: Entity Framework 4.1 Database First does not add a primary key to the DbContext T4 generated class
body: <p>I am just getting started with Entity Framework 4.1, trying out the "database first" mode.  When EF generates a Model class with the "ADO.Net DbContext Generator," shouldn't it identify the primary key for the class with a [Key] attribute?  Without this, it appears incompatible with the T4 MVCScaffolding.</p>
  
  <p>Here are the details:</p>
  
  <p>Using the Entity Data Model Designer GUI, I have added a simple "country" table to the model from my existing database.  The GUI correctly identifies a single integer identity key field named "PK" as my primary key. (Alas! I'm a new user so I can't add a screenshot.  I've included the CSDL instead below.)  However, when EF generates code using the "ADO.Net DbContext Generator", it does not identify the PK field as the key field in the generated class (see code excerpt below).</p>
  
  <p>The CSDL for the "country" table:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;edmx:ConceptualModels&gt;
    &lt;Schema Namespace="EpiDataModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"&gt;
      &lt;EntityContainer Name="EpiModelEntities" annotation:LazyLoadingEnabled="true"&gt;
        &lt;EntitySet Name="countries" EntityType="EpiDataModel.country" /&gt;
      &lt;/EntityContainer&gt;
      &lt;EntityType Name="country"&gt;
        &lt;Key&gt;
          &lt;PropertyRef Name="PK" /&gt;
        &lt;/Key&gt;
        &lt;Property Name="PK" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" /&gt;
        &lt;Property Name="Abbreviation" Type="String" Nullable="false" MaxLength="200" Unicode="false" FixedLength="false" /&gt;
        &lt;Property Name="Name" Type="String" MaxLength="1024" Unicode="false" FixedLength="false" /&gt;
        &lt;Property Name="Description" Type="String" MaxLength="1024" Unicode="false" FixedLength="false" /&gt;
        &lt;Property Name="Sequence" Type="Int32" /&gt;
      &lt;/EntityType&gt;
    &lt;/Schema&gt;
  &lt;/edmx:ConceptualModels&gt;
  </code></pre>
  
  <p>Here's the autogenerated code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //    This code was generated from a template.
  //
  //    Manual changes to this file may cause unexpected behavior in your application.
  //    Manual changes to this file will be overwritten if the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  using System;
  using System.Collections.Generic;
  
  namespace MvcApplication1.Areas.Epi.Models
  {
      public partial class country
      {
          public int PK { get; set; }
          public string Abbreviation { get; set; }
          public string Name { get; set; }
          public string Description { get; set; }
          public Nullable&lt;int&gt; Sequence { get; set; }
      }
  }
  </code></pre>
  
  <p>This causes a problem when I try to scaffold a controller using the MVCScaffolding T4 template.  I get an error "No properties appear to be primary keys."  The command and output from the NuGet Package Manager Console is below:</p>
  
  <pre><code>PM&gt; scaffold controller MvcApplication1.Areas.Epi.Models.country -Area Epi -NoChildItems -DbContextType MvcApplication1.Areas.Epi.Models.EpiModelEntities -Force
  Scaffolding countriesController...
  Get-PrimaryKey : Cannot find primary key property for type 'MvcApplication1.Areas.Epi.Models.country'. No properties appear to be primary keys.
  At C:\work\EPI\EPIC_MVC3\sandbox\MvcApplication1\packages\MvcScaffolding.1.0.6\tools\Controller\MvcScaffolding.Controller.ps1:74 char:29
  + $primaryKey = Get-PrimaryKey &lt;&lt;&lt;&lt;  $foundModelType.FullName -Project $Project -ErrorIfNotFound
      + CategoryInfo          : NotSpecified: (:) [Get-PrimaryKey], Exception
      + FullyQualifiedErrorId : T4Scaffolding.Cmdlets.GetPrimaryKeyCmdlet
  </code></pre>
  
  <p>However, if I manually change the generated class to add a [Key] attribute to the field, then the exact same scaffolding command shown above works fine:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations; // manually added
  
  namespace MvcApplication1.Areas.Epi.Models
  {
      public partial class country
      {
          [Key]                        // manually added
          public int PK { get; set; }
          public string Abbreviation { get; set; }
          public string Name { get; set; }
          public string Description { get; set; }
          public Nullable&lt;int&gt; Sequence { get; set; }
      }
  }
  </code></pre>
  
  <p>So why aren't EF Database First and the T4 MVCScaffolding playing nice together?  And even without the scaffolding issue, don't the EF classes need to know what the key field(s) are?  </p>