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

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

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

问题

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

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

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

  1. namespace DataLayer.Models
  2. {
  3. public class QuizType
  4. {
  5. [Key]
  6. [Required]
  7. public int quizType { get; set; }
  8. [MaxLength(150)]
  9. public string QuizTitle { get; set; }
  10. public QuizModel quizModel { get; set; }
  11. }
  12. }

UserModel:

  1. namespace DataLayer.Models
  2. {
  3. public class UserModel
  4. {
  5. [Key]
  6. [Required]
  7. public int UserID { get; set; }
  8. [Required]
  9. [MaxLength(300)]
  10. public string UserName { get; set; }
  11. [Required]
  12. [MaxLength(300)]
  13. public string Password { get; set; }
  14. [AllowNull]
  15. [MaxLength(150)]
  16. public string Role { get; set; }
  17. public ICollection<QuizModel> quizes { get; set; }
  18. }
  19. }

QuizModel:

  1. namespace DataLayer.Models
  2. {
  3. public class QuizModel
  4. {
  5. [Key]
  6. [Required]
  7. public int QuizID { get; set; }
  8. [Required]
  9. [MaxLength(150)]
  10. public string QuizName { get; set; }
  11. [Required]
  12. [MaxLength(300)]
  13. public string Categories { get; set; }
  14. [Required]
  15. public int QuestionCount { get; set; }
  16. [Required]
  17. public int QuizType { get; set; }
  18. [ForeignKey("QuizType")]
  19. public QuizType quizType { get; set; }
  20. [Required]
  21. [ForeignKey("User")]
  22. public int UserID { get; set; }
  23. public UserModel User { get; set; }
  24. public ICollection<QuestionModel> Questions { get; set; }
  25. }
  26. }

QuestionModel:

  1. namespace DataLayer.Models
  2. {
  3. public class QuestionModel
  4. {
  5. [Key]
  6. [Required]
  7. public int QuestionID { get; set; }
  8. [Required]
  9. [MaxLength(400)]
  10. public string QuestionText { get; set; }
  11. [Required]
  12. public int QuizType{ get; set; }
  13. [ForeignKey(name:"quiz")]
  14. public int QuizID { get; set; }
  15. public QuizModel quiz { get; set; }
  16. public AnswerModel Answer { get; set; }
  17. public ICollection<AnswerOption> Options { get; set; }
  18. }
  19. }

AnswerModelAnswerOption类:

  1. namespace DataLayer.Models
  2. {
  3. public class AnswerModel
  4. {
  5. [Key]
  6. [Required]
  7. public int AnswerID { get; set; }
  8. [Required]
  9. [MaxLength(300)]
  10. public string AnswerText { get; set; }
  11. [Required]
  12. [ForeignKey(name: "Question")]
  13. public int QuestionID { get; set; }
  14. public QuestionModel Question { get; set; }
  15. [Required]
  16. public bool AnswerValidation { get; set; }
  17. }
  18. public class AnswerOption
  19. {
  20. [Key]
  21. [Required]
  22. public int OptionID { get; set; }
  23. [Required]
  24. [MaxLength(300)]
  25. public string OptionText { get; set; }
  26. [Required]
  27. public bool AnswerValidation { get; set; }
  28. [Required]
  29. [ForeignKey(name: "questionModel")]
  30. public int QuestionID { get; set; }
  31. public QuestionModel QuestionModel { get; set; }
  32. }
  33. }
英文:

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:

  1. namespace DataLayer.Models
  2. {
  3. public class QuizType
  4. {
  5. [Key]
  6. [Required]
  7. public int quizType { get; set; }
  8. [MaxLength(150)]
  9. public string QuizTitle { get; set; }
  10. public QuizModel quizModel { get; set; }
  11. }
  12. }

UserModel:

  1. namespace DataLayer.Models
  2. {
  3. public class UserModel
  4. {
  5. [Key]
  6. [Required]
  7. public int UserID { get; set; }
  8. [Required]
  9. [MaxLength(300)]
  10. public string UserName { get; set; }
  11. [Required]
  12. [MaxLength(300)]
  13. public string Password { get; set; }
  14. [AllowNull]
  15. [MaxLength(150)]
  16. public string Role { get; set; }
  17. public ICollection&lt;QuizModel&gt; quizes { get; set; }
  18. }
  19. }

QuizModel:

  1. namespace DataLayer.Models
  2. {
  3. public class QuizModel
  4. {
  5. [Key]
  6. [Required]
  7. public int QuizID { get; set; }
  8. [Required]
  9. [MaxLength(150)]
  10. public string QuizName { get; set; }
  11. [Required]
  12. [MaxLength(300)]
  13. public string Categories { get; set; }
  14. [Required]
  15. public int QuestionCount { get; set; }
  16. [Required]
  17. public int QuizType { get; set; }
  18. [ForeignKey(&quot;QuizType&quot;)]
  19. public QuizType quizType { get; set; }
  20. [Required]
  21. [ForeignKey(&quot;User&quot;)]
  22. public int UserID { get; set; }
  23. public UserModel User { get; set; }
  24. public ICollection&lt;QuestionModel&gt; Questions { get; set; }
  25. }
  26. }

QuestionModel:

  1. namespace DataLayer.Models
  2. {
  3. public class QuestionModel
  4. {
  5. [Key]
  6. [Required]
  7. public int QuestionID { get; set; }
  8. [Required]
  9. [MaxLength(400)]
  10. public string QuestionText { get; set; }
  11. [Required]
  12. public int QuizType{ get; set; }
  13. [ForeignKey(name:&quot;quiz&quot;)]
  14. public int QuizID { get; set; }
  15. public QuizModel quiz { get; set; }
  16. public AnswerModel Answer { get; set; }
  17. public ICollection&lt;AnswerOption&gt; Oprtions { get; set; }
  18. }
  19. }

AnswerModel and AnswerOption classes:

  1. namespace DataLayer.Models
  2. {
  3. public class AnswerModel
  4. {
  5. [Key]
  6. [Required]
  7. public int AnswerID { get; set; }
  8. [Required]
  9. [MaxLength(300)]
  10. public string Answertxt { get; set; }
  11. [Required]
  12. [ForeignKey(name: &quot;Question&quot;)]
  13. public int QuestionID { get; set; }
  14. public QuestionModel Question { get; set; }
  15. [Required]
  16. public bool AnswerValidation { get; set; }
  17. }
  18. public class AnswerOption
  19. {
  20. [Key]
  21. [Required]
  22. public int OptionID { get; set; }
  23. [Required]
  24. [MaxLength(300)]
  25. public string Optiontxt { get; set; }
  26. [Required]
  27. public bool answerValidation { get; set; }
  28. [Required]
  29. [ForeignKey(name: &quot;questionModel&quot;)]
  30. public int QuestionID { get; set; }
  31. public QuestionModel questionModel { get; set; }
  32. }
  33. }

答案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:

确定