.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対多の関係を持っているが、私はそれがあるとは思いません何がエラーの原因です。
ご協力いただければ幸いです。
ありがとうございました。
返信ありがとうございました@Massimiliano。唯一の問題は、特定の質問に対する回答を取得するためにquestionAnswerでQuestionIDを使用していて、IDなしではできないことです。私はそれを行うことができる別の方法がない限り?また、「Viewer.Models.QuestionAnswer」と「Viewer.Models.Question」の間の関連付けの主要な終わりを特定できません。この関連付けの主な終点は、流暢なAPIまたはデータアノテーションのどちらかの関係を使用して明示的に設定する必要があります。 ' –
私は自分の答えを編集しました。 –
ありがとうございました。私はしばしば流暢なAPIと混乱するが、私は今それを理解し始めている。午前中に仕事に出てすぐに働くことがうれしい! :-) –