C# 无效的列名';QuestionGroup_QuestionGroupID';
我有一个.NET,MVC5,EF6代码优先的项目。基本上,在使用EF将对象保存到数据库时,我得到了C# 无效的列名';QuestionGroup_QuestionGroupID';,c#,asp.net-mvc,entity-framework,entity-framework-6,C#,Asp.net Mvc,Entity Framework,Entity Framework 6,我有一个.NET,MVC5,EF6代码优先的项目。基本上,在使用EF将对象保存到数据库时,我得到了{“无效列名'QuestionGroup\u QuestionGroupID.”}QuestionGroup是模型,它与具有外键的子模型有关系QuestionGroupID。其键的名称是QuestionGroupID,在模型及其对应的表中都是 模型: public class QuestionGroup { [Key] [Required] public Int32 Ques
{“无效列名'QuestionGroup\u QuestionGroupID.”}
QuestionGroup
是模型,它与具有外键的子模型有关系QuestionGroupID
。其键的名称是QuestionGroupID
,在模型及其对应的表中都是
模型:
public class QuestionGroup
{
[Key]
[Required]
public Int32 QuestionGroupID { get; set; }
//Other properties omitted for brevity
public Guid MasteryObjectiveID { get; set; }
public virtual MasteryObjective MasteryObjective {
//Necessary because this model is in a different context/db and EF doesn't support that.
get { return DAL.DatabaseMethods.GetMasteryObjective(MasteryObjectiveID); }
}
public virtual List<MultipleChoiceQuestion> Questions{ get; set; }
}
我得到的外部异常是用户代码未处理的DbUpdateException,而内部异常是无效的列名“QuestionGroup\u QuestionGroupID”。
这个错误是从数据库方法保存它的地方抛出的,就像
context.QuestionGroup.Add(qg); context.SaveChanges();
这种方法已经有一段时间没有改变了,我很确定那里没有什么问题
下一步应该尝试什么?数据库中可能不存在该列。如果您使用的是迁移,请尝试从包管理器中运行updatedatabase
它也可能有助于浏览数据库,验证其命名是否正确。如果是其他东西,您可以这样装饰钥匙:
[Key]
[Required]
[Column("QuestionGroupID")] // Change this to whatever the column is named
public Int32 QuestionGroupID { get; set; }
正如我所说,该列在数据库表中的命名是正确的;我已经查过了。
[Key]
[Required]
[Column("QuestionGroupID")] // Change this to whatever the column is named
public Int32 QuestionGroupID { get; set; }