2

MVC脚手架错误我想弄清楚如何使MVC脚手架与复合/复合键合作。MVC脚手架错误EF 4.5代码首先

我有如下表:

public class Data 
{ 
    [Key, Column(Order = 0)] 
    [ForeignKey("Note")] 
    [Display(Name = "Note id")] 
    public int NoteId { get; set; } 

    [Key, Column(Order = 1)] 
    [ForeignKey("Member")] 
    [Display(Name = "Member id")] 
    public int MemberId { get; set; } 

    [Display(Name = "Description")] 
    public string Description { get; set; } 

    [Display(Name = "Note")] 
    public virtual Note Note { get; set; } 

    [Display(Name = "Member")] 
    public virtual Member Member { get; set; } 
} 

当我执行脚手架行:

Scaffold Controller Data -Repository 

我收到以下错误:

Get-PrimaryKey : Cannot find primary key property for type 
Pro.Web.Models.Data'. Multiple properties appear to be 
         primary keys: NoteId, MemberId 

可能是什么解决方案对于这个问题?我使用Visual Studio 2012.

谢谢。

+0

?这是史蒂文桑德森的吗? – 2013-03-26 11:43:42

+0

是的,MvcScaffolding Steve Sanderson。我已经通过NuGet安装它。 – Cristiano 2013-03-26 12:06:14

回答

5

T4Scaffolding.Core.PrimaryKeyLocators命名空间下的类PrimaryKeyLocation具有在PrimaryKeyLocation.cs文件本身上实现的IPrimaryKeyLocator接口的列表。

读五个可用的实现,一看就知道你的代码将落在KeyAttributePropertyLocator执行返回两个部件标有[关键] attriubute,而是从T4发动机运行GetPrimaryKeyCmdlet.cs和调用PrimaryKeyLocation类有下面的实现:

switch (primaryKeyProperties.Count) 
{ 
    case 0: 
     // Code when no key is found 
    case 1: 
     // Code when one key is found 
    default: 
     // Code when more than one key is found 
     WriteError(string.Format("Cannot find primary key property for type '{0}'. 
       Multiple properties appear to be primary keys: {1}", 
        foundClass.FullName, primaryKeyPropertyNames)); 
} 

因此,作为switch语句不超过一个键处理,组合键不支持。解决这个问题的一个办法就是实现复合键的情况,但我不知道它对t4模板本身的影响。

Source code for the scaffolding tool.

您正在使用什么工具脚手架
+0

谢谢。我害怕这样的事会成为一个问题。所以唯一的解决办法是手动完成所有的事情。 – Cristiano 2013-03-26 13:10:36

+1

好了,解决这个问题的另一种方法是仅模拟一个键(从第二个属性中删除键),以创建您的控制器和视图,然后继续修改这些文件,而不是从头创建所有 – 2013-03-26 13:41:30