在 EF Core 的 Code-First 中运行 Add-Migration 时出现错误。

huangapple go评论74阅读模式
英文:

Get error when running Add-Migration in EF Core code-first

问题

我经常在互联网上搜索并向ChatGPT提问,但是我找不到答案。当我运行add-migration时,出现了错误:

无法从实体类型DataLayer.Models.QuizModel (Dictionary<string, object>)中删除属性quizType,因为它在外键{ 'quizType' }上被使用。在删除属性之前,必须删除或重新定义所有包含的外键。

但是我没有删除任何东西,我认为错误是错误的。这是我的quiz上下文和模型:

namespace DataLayer.Models
{ 
    public class QuizType
    {
        [Key]
        [Required]
        public int quizType { get; set; }

        [MaxLength(150)]
        public string QuizTitle { get; set; }

        public QuizModel quizModel { get; set; }
    }
}

UserModel:

namespace DataLayer.Models
{
    public class UserModel
    {
        [Key]
        [Required]
        public int UserID { get; set; }

        [Required]
        [MaxLength(300)]
        public string UserName { get; set; }

        [Required]
        [MaxLength(300)]
        public string Password { get; set; }

        [AllowNull]
        [MaxLength(150)]
        public string Role { get; set; }

        public ICollection<QuizModel> quizes { get; set; }
    }
}

QuizModel:

namespace DataLayer.Models
{
    public class QuizModel
    {
        [Key]
        [Required]
        public int QuizID { get; set; }

        [Required]
        [MaxLength(150)]
        public string QuizName { get; set; }

        [Required]
        [MaxLength(300)]
        public string Categories { get; set; }

        [Required]
        public int QuestionCount { get; set; }

        [Required]
        public int QuizType { get; set; }

        [ForeignKey("QuizType")]
        public QuizType quizType { get; set; }

        [Required]
        [ForeignKey("User")]
        public int UserID { get; set; }

        public UserModel User { get; set; }

        public ICollection<QuestionModel> Questions { get; set; }
    }
}

QuestionModel:

namespace DataLayer.Models
{
    public class QuestionModel
    {
        [Key]
        [Required]
        public int QuestionID { get; set; }

        [Required]
        [MaxLength(400)]
        public string QuestionText { get; set; }

        [Required]
        public int QuizType{ get; set; }

        [ForeignKey(name:"quiz")]
        public int QuizID { get; set; }

        public QuizModel quiz { get; set; } 

        public AnswerModel Answer { get; set; }

        public ICollection<AnswerOption> Options { get; set; }
    }
}

AnswerModelAnswerOption类:

namespace DataLayer.Models
{
    public class AnswerModel
    {
        [Key]
        [Required]
        public int AnswerID { get; set; }

        [Required]
        [MaxLength(300)]
        public string AnswerText { get; set; }

        [Required]
        [ForeignKey(name: "Question")]
        public int QuestionID { get; set; }

        public QuestionModel Question { get; set; }

        [Required]
        public bool AnswerValidation { get; set; }
    }

    public class AnswerOption
    {
        [Key]
        [Required]
        public int OptionID { get; set; }

        [Required]
        [MaxLength(300)]
        public string OptionText { get; set; }

        [Required]
        public bool AnswerValidation { get; set; }

        [Required]
        [ForeignKey(name: "questionModel")]
        public int QuestionID { get; set; }

        public QuestionModel QuestionModel { get; set; }
    }
}
英文:

I search internet a lot and ask question form chatgpt a lot, too, but I can't find my answer. I got error when I run add-migration:

> The property 'quizType' cannot be removed from entity type 'DataLayer.Models.QuizModel (Dictionary<string, object>)' because it is being used in the foreign key {'quizType'} on 'DataLayer.Models.QuizModel (Dictionary<string, object>)'. All containing foreign keys must be removed or redefined before the property can be removed.

But I don't removed anything I think the error is wrong this is my quiz context and my model:

namespace DataLayer.Models
{ 
    public class QuizType
    {
        [Key]
        [Required]
        public int quizType { get; set; }

        [MaxLength(150)]
        public string QuizTitle { get; set; }

        public QuizModel quizModel { get; set; }
    }
}

UserModel:

namespace DataLayer.Models
{
    public class UserModel
    {
        [Key]
        [Required]
        public int UserID { get; set; }

        [Required]
        [MaxLength(300)]
        public string UserName { get; set; }

        [Required]
        [MaxLength(300)]
        public string Password { get; set; }

        [AllowNull]
        [MaxLength(150)]
        public string Role { get; set; }

        public ICollection&lt;QuizModel&gt; quizes { get; set; }
    }
}

QuizModel:

namespace DataLayer.Models
{
    public class QuizModel
    {
        [Key]
        [Required]
        public int QuizID { get; set; }

        [Required]
        [MaxLength(150)]
        public string QuizName { get; set; }

        [Required]
        [MaxLength(300)]
        public string Categories { get; set; }

        [Required]
        public int QuestionCount { get; set; }

        [Required]
        public int QuizType { get; set; }

        [ForeignKey(&quot;QuizType&quot;)]
        public QuizType quizType { get; set; }

        [Required]
        [ForeignKey(&quot;User&quot;)]
        public int UserID { get; set; }

        public UserModel User { get; set; }

        public ICollection&lt;QuestionModel&gt; Questions { get; set; }
    }
}

QuestionModel:

namespace DataLayer.Models
{
    public class QuestionModel
    {
        [Key]
        [Required]
        public int QuestionID { get; set; }

        [Required]
        [MaxLength(400)]
        public string QuestionText { get; set; }

        [Required]
        public int QuizType{ get; set; }

        [ForeignKey(name:&quot;quiz&quot;)]
        public int QuizID { get; set; }

        public QuizModel quiz { get; set; } 

        public AnswerModel Answer { get; set; }

        public ICollection&lt;AnswerOption&gt; Oprtions { get; set; }
    }
}

AnswerModel and AnswerOption classes:

namespace DataLayer.Models
{
    public class AnswerModel
    {
        [Key]
        [Required]
        public int AnswerID { get; set; }

        [Required]
        [MaxLength(300)]
        public string Answertxt { get; set; }

        [Required]
        [ForeignKey(name: &quot;Question&quot;)]
        public int QuestionID { get; set; }

        public QuestionModel Question { get; set; }

        [Required]
        public bool AnswerValidation { get; set; }
    }

    public class AnswerOption
    {
        [Key]
        [Required]
        public int OptionID { get; set; }

        [Required]
        [MaxLength(300)]
        public string Optiontxt { get; set; }

        [Required]
        public bool answerValidation { get; set; }

        [Required]
        [ForeignKey(name: &quot;questionModel&quot;)]
        public int QuestionID { get; set; }

        public QuestionModel questionModel { get; set; }
    }
}

答案1

得分: 0

你正在为"QuizType"类的主键使用错误的名称,应该使用"QuizTypeID"或"ID"来解决这个问题,而不是只使用"quizType"。

英文:

You are doing wrong name for "QuizType" class Primary Key instead of just "quizType" you should do "QuizTypeID" or "ID" and this will resolve the issue.

huangapple
  • 本文由 发表于 2023年8月9日 00:22:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定