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