我是新来的.NET MVC,我很努力地使用Code First与现有的数据库,其中表中有一个到一个非 - 或1(1→0..1)的关系。在ASP.NET MVC中的一对一或非一对一的关系EF代码优先
我有一个报告,可以有很多部分,每个部分可以有很多问题。现在,我认为我遇到麻烦了......每个问题可能只有一个答案,或者没有答案。
,我发现了以下错误:
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 �*�.
这里是我的模型类:
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
public class Section
{
[Key]
public int SectionID { get; set; }
public string SectionEnglish { get; set; }
public string SectionWelsh { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
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和审计一个一对多的关系,但我不认为这是是什么导致了错误。
任何帮助,将不胜感激。
谢谢。
感谢您的回复@Massimiliano。唯一的麻烦是,我在QuestionAnswer中使用QuestionID来检索特定问题的答案,但没有ID,我无法做到这一点。除非有另一种方法可以做到这一点?此外,我似乎得到的错误:'无法确定类型'Viewer.Models.QuestionAnswer'和'Viewer.Models.Question'之间的关联的主要结束。该关联的主要目的必须使用关系流畅API或数据注释来显式配置。' – 2012-02-14 16:26:46
我编辑了我的答案。 – 2012-02-14 16:41:09
非常感谢。我经常对流利的API感到困惑,但我现在开始了解它。很高兴在早上进来工作,并立即开始工作! :-) – 2012-02-15 09:10:13