我想显示与以下 类 的关系,但出现这些错误
I want to show relationship with following classes but I got these error
public class Newtask
{
[Key]
public int TId { get; set; }
public string Name { get; set; }
public int Estimated_days_of_work { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
public virtual ICollection<Subtask> Subtasks { get; set; }
}
public class Subtask
{
[Key]
public int SId { get; set; }
public string SubName { get; set; }
public int SEstimated_days_of_work { get; set; }
public int NewtaskTId { get; set; }
public virtual Newtask Newtasks { get; set; }
}
public class SubSubtask
{
[Key]
public int SsId { get; set; }
public string SubSubTaskName { get; set; }
public int SsEstimated_days_of_work { get; set; }
public int NewtaskTId { get; set; }
public int SId { get; set; }
public virtual Newtask Newtasks { get; set; }
public virtual Subtask Subtasks { get; set; }
}
现在我有这些 类,我想显示它们之间的关系,就像每个任务都有子任务,子任务也有它的子任务,但是当我 运行 应用程序时,我得到了这些错误:
System.Data.SqlClient.SqlException: 'Introducing FOREIGN KEY constraint 'FK_dbo.SubSubtasks_dbo.Subtasks_SId' on table 'SubSubtasks' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.'
如何修改此关系?
您的 classes 正在产生循环依赖,因此当您要删除某些实体时,它会因多个错误而中断。如果您忽略删除实体,您可以将其添加到您的 dbcontext class 的模型构建器中。
protected override void OnModelCreating(ModelBuilder modelbuilder)
{
foreach (var relationship in modelbuilder.Model.GetEntityTypes().SelectMany(e
=> e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelbuilder);
}
虽然正确的解决方案是更改不会产生循环依赖的业务模型。
如果您没有发现任何问题,您可以这样更改 classes:
public class Newtask
{
[Key]
public int TId { get; set; }
public string Name { get; set; }
public int Estimated_days_of_work { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
public virtual ICollection<Subtask> Subtasks { get; set; }
}
public class Subtask
{
[Key]
public int SId { get; set; }
public string SubName { get; set; }
public int SEstimated_days_of_work { get; set; }
public int NewtaskTId { get; set; }
public virtual Newtask Newtasks { get; set; }
public virtual Substask HasSubtask{get;set;}
}
并将 dbset 中的模型构建器更改为:
modelbuilder.Entity(typeof (Subtask))
.OnDelete(DeleteBehavior.Restrict);
public class Newtask
{
[Key]
public int TId { get; set; }
public string Name { get; set; }
public int Estimated_days_of_work { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
public virtual ICollection<Subtask> Subtasks { get; set; }
}
public class Subtask
{
[Key]
public int SId { get; set; }
public string SubName { get; set; }
public int SEstimated_days_of_work { get; set; }
public int NewtaskTId { get; set; }
public virtual Newtask Newtasks { get; set; }
}
public class SubSubtask
{
[Key]
public int SsId { get; set; }
public string SubSubTaskName { get; set; }
public int SsEstimated_days_of_work { get; set; }
public int NewtaskTId { get; set; }
public int SId { get; set; }
public virtual Newtask Newtasks { get; set; }
public virtual Subtask Subtasks { get; set; }
}
现在我有这些 类,我想显示它们之间的关系,就像每个任务都有子任务,子任务也有它的子任务,但是当我 运行 应用程序时,我得到了这些错误:
System.Data.SqlClient.SqlException: 'Introducing FOREIGN KEY constraint 'FK_dbo.SubSubtasks_dbo.Subtasks_SId' on table 'SubSubtasks' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.'
如何修改此关系?
您的 classes 正在产生循环依赖,因此当您要删除某些实体时,它会因多个错误而中断。如果您忽略删除实体,您可以将其添加到您的 dbcontext class 的模型构建器中。
protected override void OnModelCreating(ModelBuilder modelbuilder)
{
foreach (var relationship in modelbuilder.Model.GetEntityTypes().SelectMany(e
=> e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelbuilder);
}
虽然正确的解决方案是更改不会产生循环依赖的业务模型。 如果您没有发现任何问题,您可以这样更改 classes:
public class Newtask
{
[Key]
public int TId { get; set; }
public string Name { get; set; }
public int Estimated_days_of_work { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
public virtual ICollection<Subtask> Subtasks { get; set; }
}
public class Subtask
{
[Key]
public int SId { get; set; }
public string SubName { get; set; }
public int SEstimated_days_of_work { get; set; }
public int NewtaskTId { get; set; }
public virtual Newtask Newtasks { get; set; }
public virtual Substask HasSubtask{get;set;}
}
并将 dbset 中的模型构建器更改为:
modelbuilder.Entity(typeof (Subtask))
.OnDelete(DeleteBehavior.Restrict);