2016-10-05 2 views
1

먼저 엔티티 프레임 워크 코드를 사용하고 있으며 클래스가 다른 클래스 목록을 가져야하는 클래스 목록이있는 클래스를 추가했지만 마이그레이션을 통해 데이터베이스를 업데이트하려고 할 때 나는 이것을 얻는다 :외래 키 속성이 유효하지 않습니까? (엔터티 프레임 워크 코드를 먼저)

'SubscaleScore'유형의 'SubscaleStatistics'속성에 대한 ForeignKeyAttribute가 유효하지 않습니다. 종속 키 유형 'SubscaleScore'에서 외래 키 이름 'SubscaleStatisticsId'를 찾을 수 없습니다. Name 값은 쉼표로 구분 된 외래 키 특성 이름 목록이어야합니다.

public class ExamStatistics : StatisticsData 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 
    [Required] 
    public int TestId { get; set; } 
    public IList<SubscaleStatistics> Subscales { get; set; } 
} 

public class SubscaleStatistics 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int SubscaleStatisticsId { get; set; } 
    public int TestId { get; set; } 
    public int SubscaleNumber { get; set; } 
    public int ExamStatisticsId { get; set; } 

    [ForeignKey("ExamStatisticsId")] 
    public virtual ExamStatistics ExamStatistics { get; set; } 

    public IList<SubscaleScore> SubscaleScores { get; set; } 
} 

public class SubscaleScore 
{ 
    public int TestId { get; set; } 
    public int Subscale { get; set; } 
    public double Score { get; set; } 
    public int SubscaleId { get; set; } 

    [ForeignKey("SubscaleStatisticsId")] 
    public virtual SubscaleStatistics SubscaleStatistics { get; set; } 

} 

내가 잘못 여기서 뭐하는 거지 :

여기 내 수업 모습 무엇인가? 또는 잘못된 정보를 얻기 위해 더 많은 정보를 제공해야합니까?

+1

'SubscaleStatisticsId' 속성을'SubscaleScore '에 추가하려고 했습니까? '클래스는 오류 상태입니까? –

답변

2

외래 키 속성을 SubscaleScore에 추가해야합니다.

public int SubscaleStatisticsId { get; set; } 

는 튜토리얼 여기를 참조하십시오 foreign keys

1

는 외래 키가 ID가 아닌 객체 여야합니다

이런 식으로 뭔가를 시도 :

public class SubscaleScore 
{ 
    public int TestId { get; set; } 
    public int Subscale { get; set; } 
    public double Score { get; set; } 
    public int SubscaleId { get; set; } 

    [ForeignKey("SubscaleStatisticsId")] 
    public int SubscaleStatisticsId { get; set; } 

    public virtual SubscaleStatistics SubscaleStatistics { get; set; } 

} 

는 외래 키를 가진 모든 클래스에 대해 동일한 작업을 수행