我的数据库结构如下:Categories表,Questions表和Answers表。问题具有类别ID,答案具有答案ID。尝试访问视图中的导航属性“答案”时,出现以下错误:
“在EntityFramework.SqlServer.dll中发生“ System.Data.Entity.Core.EntityCommandExecutionException”,但未在用户代码中处理”,伴随有:
“无效的列名'Question_ID'。”
这是我的班级样子:
public class Category
{
public int ID { get; set; }
public string Text { get; set; }
public string Description { get; set; }
public virtual List<Question> Questions { get; set; }
}
提出问题:
public class Question
{
public int ID { get; set; }
public int CategoryID { get; set; }
public string Text { get; set; }
public virtual List<Answer> Answers { get; set; }
public virtual Category Category { get; set; }
}
并回答:
public class Answer
{
public int ID { get; set; }
public int QuestionID { get; set; }
public string Text { get; set; }
public int Correct { get; set; }
public virtual Question Question { get; set; }
}
我的数据库使用完全相同的字段名称,并应用了外键。我在名称上尝试了多种变体,包括将外键更改为“ Question_ID”和“ Category_ID”,但没有运气。我也尝试过这种解决方案:Code First约定混乱,但是没有用。有人知道我要去哪里错吗?
我相信问题出在您的答案课上。试试这个...
public class Answer
{
public int ID { get; set; }
public string Text { get; set; }
public int Correct { get; set; }
[ForeignKey("Question")]
public int QuestionID { get; set; }
public virtual Question Question { get; set; }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句