ASP.NET MVC EF Code First中的一对一或非一对一关系

Gar*_*wis 0 c# entity-framework ef-code-first

我是.NET MVC的新手,我正在努力将Code First与现有数据库结合使用,其中一个表具有一对一或一对(1 - > 0..1)的关系.

我有一份报告,可以有很多部分,每个部分都有很多问题.现在这里有点我认为我遇到了麻烦...每个问题可能有一个答案或没有答案.

我收到以下错误:

System.Data.Edm.EdmAssociationEnd :: Multiplicity在关系'QuestionAnswer_Question'中的角色'QuestionAnswer_Question_Source'中无效.由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为*.

这是我的模型类:

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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

我还与ModeratorReport和Audit有一对多的关系,但我不认为这是导致错误的原因.

任何帮助,将不胜感激.

谢谢.

Mas*_*uso 5

EF正在抱怨,因为它听起来像你正在使用FK协会 - 这意味着QuestionID是实体的属性,也有一个问题参考 - 你不能用FK协会做到这一点.如果您删除QuestionIDQuestionAnswer它应该工作

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; }
}
Run Code Online (Sandbox Code Playgroud)

我建议你使用更流畅的映射,这更清晰:

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

modelBuilder.Entity<QuestionAnswer>()
                .HasRequired(qa => qa.Question)
Run Code Online (Sandbox Code Playgroud)