C# 代码优先.NET数据库中不需要的属性
我一直在使用ASP.NETMVCWeb应用程序,但遇到了一个无法解决的问题。我有以下课程:C# 代码优先.NET数据库中不需要的属性,c#,asp.net,asp.net-mvc,ef-code-first,C#,Asp.net,Asp.net Mvc,Ef Code First,我一直在使用ASP.NETMVCWeb应用程序,但遇到了一个无法解决的问题。我有以下课程: public abstract class QuestionAbstract { public int Id { get; set; } public int ExamId { get; set; } public string Description { get; set; } public virtual Exam Exam { get; set; } publ
public abstract class QuestionAbstract
{
public int Id { get; set; }
public int ExamId { get; set; }
public string Description { get; set; }
public virtual Exam Exam { get; set; }
public virtual ICollection<AnswerAbstract> Answers { get; set; }
public virtual ICollection<Variable> Variables { get; set; }
}
public abstract class AnswerAbstract
{
[Key, Column(Order = 0)]
public int ResponseId { get; set; }
[Key, Column(Order = 1)]
public int QuestionAbstractId { get; set; }
[ForeignKey("ResponseId")]
public virtual Response Response { get; set; }
[ForeignKey("QuestionAbstractId")]
public virtual QuestionAbstract Question { get; set; }
}
如您所见,创建了一个附加属性(QuestionAbstract_Id),其功能与我编写的QuestionId完全相同(引用QuestionAbstract类)。我怀疑这与QuestionAbstract收集的答案摘要有关,但这些类型的关系以前并没有给我带来问题。知道为什么会这样吗?显然,我遗漏了其他类,因为我认为问题不在其他地方,但是如果您需要任何其他代码,请随时询问
编辑:将QuestionId更改为QuestionAbstractId。还是相同的问题。尝试更改
[ForeignKey("QuestionId")]
到
而且
public int QuestionId { get; set; }
到
尝试将[ForeignKey(“QuestionId”)]更改为[ForeignKey(“QuestionAbstractId”)],并将public int QuestionId{get;set;}更改为public int QuestionAbstractId{get;set;}正如@AzharKhorasany所提到的,您的属性和类名必须相等(QuestionAbstract外键不能命名为QuestionId,它必须命名为QuestionAbstractId)让EF正确地映射它们。只是尝试了一下,不起作用。这似乎是一个很好的猜测,虽然尝试过,但仍然是相同的结果。@JuanCarlosGuzmánIslas我已经发布了这个解决方案,你应该试试那个
[ForeignKey("QuestionAbstractId")]
public int QuestionId { get; set; }
public int QuestionAbstractId { get; set; }