2

MVC 스캐 폴딩을 복합/복합 키로 사용하는 방법을 알아 내려고합니다.EF 4.5 코드가 포함 된 복합 키 MVC 스캐 폴딩 오류 처음으로

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

해결책이 될 수 무엇 :

Scaffold Controller Data -Repository 

나는 다음과 같은 오류가 나타납니다 내가 비계에 대한 행을 실행하면

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; } 
} 

:

나는 다음과 같은 테이블이 이 문제는? Visual Studio 2012를 사용합니다.

감사합니다.

+0

어떤 스캐 폴딩 도구를 사용하고 있습니까? 스티븐 샌더슨 출신인가요? –

+0

네, 스티브 샌더슨의 Mvc 받침. NuGet을 통해 설치했습니다. – Cristiano

답변

5

T4Scaffolding.Core.PrimaryKeyLocators 네임 스페이스 아래 클래스 PrimaryKeyLocation에는 PrimaryKeyLocation.cs 파일 자체에 구현 된 IPrimaryKeyLocator 인터페이스 목록이 있습니다.

다섯 구현 가능한 읽기, 하나는 코드가 모두 회원은 [키] attriubute 표시가 반환하는 KeyAttributePropertyLocator 구현에 떨어질 것이라고 이야기하지만, 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)); 
} 

따라서, 하나 이상의 키를 처리하지 않는 스위치 문으로, 복합 키는를 지원하지 않습니다. 이 중 하나의 방법은 복합 키의 경우를 구현하는 것이지만 t4 템플릿 자체에 미치는 영향을 알지는 못합니다.

Source code for the scaffolding tool.

+0

고맙습니다. 나는 그런 것이 문제가 될 것 같았습니다. 그래서 유일한 해결책은 모든 것을 수동으로하는 것입니다. – Cristiano

+1

글쎄,이 문제를 해결할 수있는 또 다른 방법은 컨트롤러와 뷰를 만든 다음이 파일을 처음부터 새로 만드는 대신 하나의 키만 시뮬레이션 (두 번째 특성에서 키 제거)하는 것입니다. –