我在网上搜索了很多次,也在聊天中问了很多问题,但我找不到答案.当我运行add-migration时出现错误:

无法从实体类型""DataLayer.Models.QuizModel(Dictionary<String,Object&>)""中删除属性""quizType"",因为它正被用于""DataLayer.Models.QuizModel(Dictionary<字符串,Object&>)""的外键""quizType"".""必须先移除或重新定义包含外键的所有内容,然后才能移除该属性.

但我没有删除任何东西,我认为错误是错误的.这是我的测验环境和我的模型:

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班和AnswerOption班:

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; }
    }
}

推荐答案

您正在为"QuizType"类主键命名错误,而不仅仅是"quizType",您应该使用"QuizTypeID"或"ID",这将解决问题.

Csharp相关问答推荐

元素存在方法是否损坏

Blazor:计算值或保留为默认值

为什么在ANTLR4中会出现不匹配的输入错误?""

Polly使用泛型重试和重试包装函数

调用Task.Run()与DoSomethingAsync()有什么不同?

未在数据流块之间传播完成

为什么AggregateException的Catch块不足以处理取消?

TagHelpers在新区域不起作用

有没有更好的方法来在CosmosDB上插入非id?

.NET 6:如何防止系统生成的日志(log)?

使用C#和.NET 7.0无法访问Cookie中的数据

当try 测试具有协变返回类型的抽象属性时,类似功能引发System.ArgumentException

获取混淆&Quot;模糊引用&Quot;错误

解决方案:延长ABP框架和ANGING OpenIddict中的令牌生命周期

如何消除Visual Studio错误,因为它不识别集合表达式的新C#12语法?

.NET文档对继承的困惑

如何保存具有多个重叠图片框的图片框?

无法将.Net Framework 4.8.1升级到.Net 7

如何在C#中用Serilog记录类路径、方法名和行编号

将带有嵌套If-Else的Foreach循环转换为Linq表达式