2016-10-05 4 views
1

私はエンティティフレームワークのコードを最初に使用していますが、クラスもクラスの別のリストを持っているクラスのリストを持っているクラスを追加しましたが、私は、私はこれを取得します:外部キーのプロパティが無効ですか? (エンティティフレームワークのコードを最初に)

プロパティ 'SubscaleStatistics'のタイプのSubscaleScore 'の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

のForeignKeyに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; } 

} 

をForeignKeyの持つすべてのクラスのために同じことを行います

関連する問題