0
我無法在任何地方找到答案,所以在這裏。ef代碼第一個映射子類到單個外鍵
我有幾個子類「答案」類型。所有的答案都需要參考他們的問題。
默認情況下,當ef創建「Answers」表時,它將創建一個名爲FullNameQuestion_QuestionId的問題表的外鍵引用,以及一個名爲TextboxQuestion_QuestionId的外鍵引用。
我想要的是TextboxQuestion和FullNameQuestion都使用相同的QuestionId外鍵。我無法弄清楚如何讓映射工作。
public abstract class Question
{
public int QuestionId { get; set; }
private string Text { get; set; }
}
public class TextboxQuestion : Question
{
public string TextboxValue { get; set; }
public virtual List<TextboxAnswer> TextboxAnswers { get; set; }
}
public class FullNameQuestion : Question
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public virtual List<FullNameAnswer> FullNameAnswers { get; set; }
}
public abstract class Answer
{
public int AnswerId { get; set; }
public int UserId { get; set; }
}
public class TextboxAnswer : Answer
{
public string TextboxValue { get; set; }
}
public class FullNameAnswer:Answer
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}
我嘗試添加一個問題ID給TextboxAnswer和FullNameAnswer類這樣的,但它只是創造兩列,QuestionId和QuestionId1。有沒有辦法讓這些共享同一列?
public class TextboxAnswer : Answer
{
[ForeignKey("Question")]
public int QuestionId { get; set; }
public virtual Question Question { get; set; }
public string TextboxValue { get; set; }
}
public class FullNameAnswer:Answer
{
[ForeignKey("Question")]
public int QuestionId { get; set; }
public virtual Question Question { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}