c# - 在尝试添加迁移时,我收到以下错误,我的数据库尚未创建,所以我不知道如何启用它
问题描述
错误:
在表 'Subjects' 上引入 FOREIGN KEY 约束 'FK_dbo.Subjects_dbo.Categories_CategoryID' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
无法创建约束。请参阅以前的错误。
我不知道如何启用on delete no action
,因为尚未创建数据库
这些是实体类
public class BaseEntity
{
public int ID { get; set; }
[DisplayName("Name") ]
public string Title { get; set; }
public string Description { get; set; }
}
public class Category : BaseEntity
{
}
public class Subject : BaseEntity
{
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
}
public class Quiz
{
public int ID { get; set; }
public string Summary { get; set; }
public string QuizName { get; set; }
public int Hours { get; set; }
public int Minutes { get; set; }
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
public virtual Subject Subject { get; set; }
public int SubjectID { get; set; }
public List<Question> Questions { get; set; }
}
public class Question
{
public int ID { get; set; }
public Quiz Quiz { get; set; }
public int QuizID { get; set; }
public string question { get; set; }
public List<Options> options { get; set; }
}
public class Options
{
public int ID { get; set; }
public int QuizID { get; set; }
public Question Question { get; set; }
public int QuestionID { get; set; }
public string option { get; set; }
public bool isOption { get; set; }
}
早些时候我设置了外键 nullable ,现在它们不可为空,但我仍然得到同样的错误。
这是创建的迁移类:
public partial class intial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Categories",
c => new
{
ID = c.Int(nullable: false, identity: true),
Title = c.String(),
Description = c.String(),
})
.PrimaryKey(t => t.ID);
CreateTable(
"dbo.Options",
c => new
{
ID = c.Int(nullable: false, identity: true),
QuizID = c.Int(nullable: false),
QuestionID = c.Int(nullable: false),
option = c.String(),
isOption = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Questions", t => t.QuestionID, cascadeDelete: true)
.Index(t => t.QuestionID);
CreateTable(
"dbo.Questions",
c => new
{
ID = c.Int(nullable: false, identity: true),
QuizID = c.Int(nullable: false),
question = c.String(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Quizs", t => t.QuizID, cascadeDelete: true)
.Index(t => t.QuizID);
CreateTable(
"dbo.Quizs",
c => new
{
ID = c.Int(nullable: false, identity: true),
Summary = c.String(),
QuizName = c.String(),
Hours = c.Int(nullable: false),
Minutes = c.Int(nullable: false),
CategoryID = c.Int(nullable: false),
SubjectID = c.Int(nullable: false),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Categories", t => t.CategoryID, cascadeDelete: true)
.ForeignKey("dbo.Subjects", t => t.SubjectID, cascadeDelete: true)
.Index(t => t.CategoryID)
.Index(t => t.SubjectID);
CreateTable(
"dbo.Subjects",
c => new
{
ID = c.Int(nullable: false, identity: true),
CategoryID = c.Int(nullable: false),
Title = c.String(),
Description = c.String(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Categories", t => t.CategoryID, cascadeDelete: true)
.Index(t => t.CategoryID);
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128),
RoleId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
CreateTable(
"dbo.UserQuizDetails",
c => new
{
ID = c.Int(nullable: false, identity: true),
UserID = c.String(),
QuizID = c.Int(nullable: false),
QuestionID = c.Int(nullable: false),
OptionID = c.Int(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Options", t => t.OptionID)
.ForeignKey("dbo.Questions", t => t.QuestionID, cascadeDelete: true)
.ForeignKey("dbo.Quizs", t => t.QuizID, cascadeDelete: true)
.Index(t => t.QuizID)
.Index(t => t.QuestionID)
.Index(t => t.OptionID);
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
CreateTable(
"dbo.AspNetUserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
UserId = c.String(nullable: false, maxLength: 128),
ClaimType = c.String(),
ClaimValue = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"dbo.AspNetUserLogins",
c => new
{
LoginProvider = c.String(nullable: false, maxLength: 128),
ProviderKey = c.String(nullable: false, maxLength: 128),
UserId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
}
public override void Down()
{
DropForeignKey("dbo.AspNetUserRoles", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserLogins", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserClaims", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.UserQuizDetails", "QuizID", "dbo.Quizs");
DropForeignKey("dbo.UserQuizDetails", "QuestionID", "dbo.Questions");
DropForeignKey("dbo.UserQuizDetails", "OptionID", "dbo.Options");
DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles");
DropForeignKey("dbo.Quizs", "SubjectID", "dbo.Subjects");
DropForeignKey("dbo.Subjects", "CategoryID", "dbo.Categories");
DropForeignKey("dbo.Questions", "QuizID", "dbo.Quizs");
DropForeignKey("dbo.Quizs", "CategoryID", "dbo.Categories");
DropForeignKey("dbo.Options", "QuestionID", "dbo.Questions");
DropIndex("dbo.AspNetUserLogins", new[] { "UserId" });
DropIndex("dbo.AspNetUserClaims", new[] { "UserId" });
DropIndex("dbo.AspNetUsers", "UserNameIndex");
DropIndex("dbo.UserQuizDetails", new[] { "OptionID" });
DropIndex("dbo.UserQuizDetails", new[] { "QuestionID" });
DropIndex("dbo.UserQuizDetails", new[] { "QuizID" });
DropIndex("dbo.AspNetUserRoles", new[] { "RoleId" });
DropIndex("dbo.AspNetUserRoles", new[] { "UserId" });
DropIndex("dbo.AspNetRoles", "RoleNameIndex");
DropIndex("dbo.Subjects", new[] { "CategoryID" });
DropIndex("dbo.Quizs", new[] { "SubjectID" });
DropIndex("dbo.Quizs", new[] { "CategoryID" });
DropIndex("dbo.Questions", new[] { "QuizID" });
DropIndex("dbo.Options", new[] { "QuestionID" });
DropTable("dbo.AspNetUserLogins");
DropTable("dbo.AspNetUserClaims");
DropTable("dbo.AspNetUsers");
DropTable("dbo.UserQuizDetails");
DropTable("dbo.AspNetUserRoles");
DropTable("dbo.AspNetRoles");
DropTable("dbo.Subjects");
DropTable("dbo.Quizs");
DropTable("dbo.Questions");
DropTable("dbo.Options");
DropTable("dbo.Categories");
}
}
}
运行 update-database 命令后发生错误。
解决方案
在 Subject 和 Categorie 的关系上添加带有正确参数的 CascaseOnDelete() 方法。提供您的代码将给出更好的答案
推荐阅读
- c++ - 如何仅使用 ctrl z 结束程序
- python - Python for循环和if在文本中查找字符串
- windows - 在 postgres 12 中定期删除存档的 Wal 文件
- variables - 布尔变量作为模板参数输入
- javascript - TypeError: undefined is not an object (evlauating 'newData[0].flag')
- python - 如何将索引列表转换为python中的x,y坐标
- python - 我正在获取图像并希望将图像保存在单个 pdf 文件中
- python - 是否可以根据输入本身将 raw_input 的类型设为整数或字符串?
- android - 在约束布局中稍微向下调整我的按钮
- laravel - Blade @props 覆盖传递的属性值