|
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() {}
[Key]
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 http://stackoverflow.com/questions/4915957/using-system-componentmodel-dataannotations-with-entity-framework-4-0/.
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.
|