Scaffolding fails "Cannot find primary key..." - EF 4.2 Model First or DB First

Jan 4, 2012 at 9:35 PM

MVCScaffolding 1.0.6 fails when you try to scaffold a controller and views based on an Entity Framework 4.1/4.2 (EDMX) model entity.  Although my entity (named "query") has a clearly marked primary key field (named "PK" with the "Entity Key" property set to "True"), the scaffolding command fails.  Below is the PowerShell command line and the output.

PM> scaffold Controller queries -area Epi -force

Scaffolding queriesController...

MvcApplication1Context already has a member called 'queries'. Skipping...

Get-PrimaryKey : Cannot find primary key property for type 'MvcApplication1.Areas.Epi.Models.query'. 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 <<<<  $foundModelType.FullName -Project $Project -ErrorIfNotFound
    + CategoryInfo          : NotSpecified: (:) [Get-PrimaryKey], Exception
    + FullyQualifiedErrorId : T4Scaffolding.Cmdlets.GetPrimaryKeyCmdlet

My workaround is to manually add a [Key] data annotation to the auto-generated query class source code:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Areas.Epi.Models
    public partial class query
        public query() {}
        public int PK { get; set; }

        // ...other code snipped... 



The scaffolding command will then work.  However, this is tedious, and the above source code changes will be overwritten the next time the EDMX model is modified.  It would be far nicer if the MVCScaffolding were 'aware' of the EF ModelFirst/DBFirst field attributes.

I tried to extend the EF Model using a metadata class as described in  I was able to add the [Key] attribute within the metadata class, but with this approach MVCScaffolding still complained that it could not find the primary key (same error message as shown above).

If there is something I'm overlooking in how to make this work, I'd appreciate a response.  Otherwise, this should probably be moved to the Issue Tracker.

Jan 8, 2012 at 12:49 PM

Yes I found exact the same problem. 

Jan 26, 2012 at 3:13 PM

Yes - same here.  I tried to use a metaData buddy class

[MetadataType(typeof( xxxMetaData))]

But this isnt picked up either.  If this could be processed then all sorted - and also solves a 2nd problem I will have where I want to specify my data attributes (for scaffoldColumn etc) via a meta data class that wont be picked up.

Apr 4, 2012 at 3:49 PM

Has anyone found an answer for this? I am having the same problems.

Apr 16, 2012 at 7:46 PM

Same here and my field is CustomerID of table Customer...

Aug 8, 2012 at 7:43 PM

I've hit this issue a few times as well myself, and know how annoying it can be.  What I can tell you is that it likes the name of the Primary key column to be either "Id" or <ClassName>Id.   So for at least a quick fix, you can rename your field to be <ClassName>Id, and then render the scaffold.  Then you can go back and correct the naming convention in the .edmx and resulting files.  Yes, yes, I know it's not a graceful, complete resolution, but it will get you back up and running till a better resolution is documented.  

What do you want for nothing? A Rubber biscuit?

Sep 30, 2013 at 10:21 AM
Has any answer been found for this, apart from adding the [Key] to the EF autogenerated file, or changing the name of the PK field to follow the expected convention, i.e. ID or <Classname>Id
May 18, 2014 at 4:54 PM
Edited May 18, 2014 at 4:56 PM
Thanks guys!
These posts were useful to me
To add to VeXHarbinger's temporary solution to make it just a little more supportable...

I used a partial class of the same entity name adding a property with an "Id" in the name with the [Key] data annotation. The get and set of this new property just pulled/set the real primary key column from the generated class. Using a partial class in a separate folder got around EF code wiping out my changes.

Again... yes yes, not totally graceful, but until a better solution comes along.