Error: Introducing FOREIGN KEY constraint

WDS2022 1 Reputation point
2022-12-08T10:43:13.727+00:00

Hi everyone!

I have been trying to set up a backend system with dotnet and ran into an issue. Hope you guys could help me here. This is the situation:

There are projects, each can have multiple tasks.
Tasks can have multiple steps and "meshobjects".
Steps can also have multiple "meshobjects".

The goal is, when deleting a project, that all tasks, steps and meshobjects are deleted automatically. (Same goes for deleting tasks and steps)
Unfortunately, I am getting the following error when den updating the database:

"Introducing FOREIGN KEY constraint '3' on table 'MeshObjectTests' 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."

Here is the code I am using:

public class Project {  
[Key]  
public int ID { get; set; }  
public List<Task> Tasks = new List<Task>();  
}  
  
public class Task {  
[Key]  
public int ID { get; set; }  
public int ProjectID_FK { get; set; }  
public Project Project { get; set; }  
public ICollection<Step> Steps { get; set; }  
public ICollection<MeshObject> MeshObjectPalette { get; set; }  
}  
  
public class Step {  
[Key]  
public int ID { get; set; }  
public int TaskID_FK { get; set; }  
public Task Task { get; set; }  
public ICollection<MeshObject> MeshObjects { get; set; }  
}  

public class MeshObject {  
[Key]  
public int ID { get; set; }  
public int? StepID_FK { get; set; }  
public Step? Step { get; set; }  
public int? TaskID_FK { get; set; }  
public Task? Task { get; set; }  
}  









protected override void OnModelCreating(ModelBuilder builder) {  
base.OnModelCreating(builder);  
  
var project = builder.Entity<Project>();  
project.HasMany<Task>(p => p.Tasks)  
.WithOne(t => t.Project)  
.HasForeignKey(t => t.ProjectID_FK)  
.OnDelete(DeleteBehavior.Cascade);  
  
var task = builder.Entity<Task>();  
task.HasOne(t => t.Project)  
.WithMany(p => p.Tasks);  
task.HasMany<Step>(t => t.Steps)  
.WithOne(s => s.Task)  
.HasForeignKey(s => s.TaskID_FK)  
.OnDelete(DeleteBehavior.Cascade);  
task.HasMany<MeshObject>(t => t.MeshObjectPalette)  
.WithOne(m => m.Task)  
.HasForeignKey(m => m.TaskID_FK)  
.OnDelete(DeleteBehavior.Cascade);  
  
var step = builder.Entity<Step>();  
step.HasOne(s => s.Task)  
.WithMany(t => t.Steps)  
.HasForeignKey(s => s.TaskID_FK);  
step.HasMany<MeshObject>(s => s.MeshObjects)  
.WithOne(m => m.Step)  
.HasForeignKey(m => m.StepID_FK)  
.OnDelete(DeleteBehavior.Cascade);  
  
var meshObject = builder.Entity<MeshObject>();  
meshObject.HasOne(m => m.Step)  
.WithMany(s => s.MeshObjects);  
meshObject.HasOne(m => m.Task)  
.WithMany(t => t.MeshObjectPalette);  
}  

Thanks a lot in advance!

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lei Zhang-MSFT 16 Reputation points Microsoft Employee
    2022-12-12T06:48:28.73+00:00

    @WDS2022 , Welcome to Microsoft Q&A,
    Based on your description, I reproduced your problem .
    You could try the following steps to solve your problem.

    1. You could change .OnDelete(DeleteBehavior.Cascade) to .OnDelete(DeleteBehavior.ClientSetNull) in the Fluent API configuration.
    2. Please delete the Migrations file in your solution.
    3. Please enter the Add-Migration and Update-Database commands again.
    4. You could check this link. https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.deletebehavior?view=efcore-7.0#fields

    Hope my solution could help you.
    Best Regards,
    Lei Zhang


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments