英文:
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; }
}
}
AnswerModel
和AnswerOption
类:
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<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> 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: "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 Optiontxt { get; set; }
[Required]
public bool answerValidation { get; set; }
[Required]
[ForeignKey(name: "questionModel")]
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论