C#Entity Framework代码第一次迁移错误,虽然密钥实际定义,但没有定义密钥

Suj*_*joy 0 c# entity-framework key ef-code-first ef-migrations

我正在使用Entity Framework代码优先方法.我收到以下错误,当我运行Update-Database -script在一个空数据库,运行程序后enable-migrationsadd-migration:

standardvba.DataAccessLayer.LessonQuestion :: EntityType'LessonQuestion'没有定义键.定义此EntityType的键.
LessonQuestions:EntityType:EntitySet'LessonQuestions'基于没有定义键的类型'LessonQuestion'.

LessonQuestion.cs:

public partial class LessonQuestion
{
    public LessonQuestion()
    {
        this.LessonQuestionDetails = new List<LessonQuestionDetail>();
    } 

    [Key]
    public int QuestionID; // Key is defined

    public int OrderNumber;

    [ForeignKey("ParentQuestion")]
    public int ParentQuestionID;  

    [ForeignKey("Lesson")]
    public int LessonID { get; set; }

    [ForeignKey("ActionedBy")]    
    public int ActionedByUserID { get; set; }
    [MaxLength(1000)]
    public string Question { get; set; } 
    public bool IsApproved { get; set; }

    [Required]  // Sets the CASCADE constraint, while creating table
    public virtual CourseLesson Lesson { get; set; }

    public virtual SiteUser ActionedBy { get; set; }

    public virtual ICollection<LessonQuestionDetail> LessonQuestionDetails { get; set; }

    public virtual LessonQuestion ParentQuestion { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了删除和重新创建Migrations文件夹等选项,但这不起作用.

Anu*_*wan 5

关键需要Public Property.在你的情况下,它是一个领域.您需要将其更改为以下内容

[Key]
public int QuestionID{get;set;}
Run Code Online (Sandbox Code Playgroud)