2012-02-14 19 views
0

.NET MVCを初めて使用しています。既存のデータベースでCode Firstを使用することに苦労しています。テーブルには、 (1 - > 0..1)の関係になります。ASP.NET MVC EFコードでの1対1の関係First

多くのセクションを持つことができるレポートがあり、各セクションには多くの質問があります。ここでは私が問題に遭遇していると思うところがあります。それぞれの質問には1つの答えがあるかもしれません。

私は次のエラーを取得しています:

ModeratorReport.cs

public class ModeratorReport 
{ 
    [Key, Column(Order = 0)] 
    public int ModeratorReportID { get; set; } 

    [Key, Column(Order = 1)] 
    public string Status { get; set; } 

    public string FileYear { get; set; } 
    public string SessionCode { get; set; } 
    public string CentreNumber { get; set; } 
    public string SubjectNumber { get; set; } 
    public string PaperNumber { get; set; } 
    public string ModeratorNumber { get; set; } 
    public DateTime? DateModified { get; set; } 

    public virtual ICollection<AuditItem> Audit { get; set; } 
} 

Section.cs

:ここ

System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'QuestionAnswer_Question_Source' in relationship 'QuestionAnswer_Question'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be �*�.

は私のモデルクラスです

Question.cs

public class Question 
{ 
    [Key] 
    public int QuestionID { get; set; } 

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

    public string QuestionEnglish { get; set; } 
    public string QuestionWelsh { get; set; } 
    public string Type { get; set; } 

    public virtual Section Section { get; set; } 
    public virtual QuestionAnswer QuestionAnswer { get; set; } 
} 

QuestionAnswer.cs

public class QuestionAnswer 
{ 
    [Key] 
    public int AnswerID { get; set; } 

    [ForeignKey("ModeratorReport"), Column(Order = 0)] 
    public int ModeratorReportID { get; set; } 
    [ForeignKey("ModeratorReport"), Column(Order = 1)] 
    public string Status { get; set; } 

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

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

    public string Answer { get; set; } 

    public virtual ModeratorReport ModeratorReport { get; set; } 
    public virtual Section Section { get; set; } 
    public virtual Question Question { get; set; } 
} 

私もModeratorReportおよび監査と1対多の関係を持っているが、私はそれがあるとは思いません何がエラーの原因です。

ご協力いただければ幸いです。

ありがとうございました。

答えて

5

FKアソシエーションを使用しているように聞こえるため、EFが苦情を申し立てています。つまり、QuestionIDはエンティティのプロパティであり、質問の参照もあります.FKアソシエーションではできません。 あなたがQuestionAnswerからQuestionIDを削除した場合、それは

public class QuestionAnswer 
{ 
    [Key,ForeignKey("Question")]] 
    public int AnswerID { get; set; } 

    [ForeignKey("ModeratorReport"), Column(Order = 0)] 
    public int ModeratorReportID { get; set; } 
    [ForeignKey("ModeratorReport"), Column(Order = 1)] 
    public string Status { get; set; } 

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


    public string Answer { get; set; } 

    public virtual ModeratorReport ModeratorReport { get; set; } 
    public virtual Section Section { get; set; } 
    public virtual Question Question { get; set; } 
} 

を動作するはずです、私はより明確である流暢なマッピングを使用することができ示唆している:はい、あなたが順番に回答エンティティからQuestionIdを削除する必要があります

modelBuilder.Entity<Question>() 
      .HasOptional(q => q.QuestionAnswer) 

modelBuilder.Entity<QuestionAnswer>() 
       .HasRequired(qa => qa.Question) 
+0

返信ありがとうございました@Massimiliano。唯一の問題は、特定の質問に対する回答を取得するためにquestionAnswerでQuestionIDを使用していて、IDなしではできないことです。私はそれを行うことができる別の方法がない限り?また、「Viewer.Models.QuestionAnswer」と「Viewer.Models.Question」の間の関連付けの主要な終わりを特定できません。この関連付けの主な終点は、流暢なAPIまたはデータアノテーションのどちらかの関係を使用して明示的に設定する必要があります。 ' –

+0

私は自分の答えを編集しました。 –

+0

ありがとうございました。私はしばしば流暢なAPIと混乱するが、私は今それを理解し始めている。午前中に仕事に出てすぐに働くことがうれしい! :-) –

1

をそれを働かせるために。 流暢な設定はその後、次のようになります。あなたは通信員の質問を取得するために、回答にナビゲーションプロパティを使用したい場合は

また
modelBuilder.Entity<Question>() 
      .HasOptional(qa => qa.QuestionAnswer) 

、私は答えは質問せずに存在するべきではないと思います。次に、QuestionIdを使用して、回答のIDをPKとFKの両方に強制することができます。 これを回答の構成に追加してください。

modelBuilder.Entity<QuestionAnswer>() 
       .HasRequired(qa => qa.Question) 
関連する問題