Home > other >  Cascading Deletes when using EF Core and Models with inheritance
Cascading Deletes when using EF Core and Models with inheritance

Time:10-07

I am trying to make some changes to an EF model and Im running into an issue with cascading deletes, I know I have done this before but cannot find where and cannot find a resolution that I'm happy with.

This is an example of the existing model, I've tried to use something generic that we will all recognise the real world relationships between (schools and students/teachers):

namespace Models
{
    public class School
    {
        public guid Id { get; set; }

        public string Name {get; set;}

        public IList<Teacher> Teachers { get; set; }
    }

    public class Person
    {
        public guid Id { get; set; }

        public string Name { get; set; }

        public guid SchoolId SchoolId { get; set; }

        public School School { get; set; }
   
        public string TeachersUnionId { get; set; }
    }    
}

This gives us a School table in the database with a one to many relationship onto the person table.

What we want to do is make the person less generic and split them into teacher objects and student objects.

Im doing this like so :

namespace Models
{
    public class School
    {
        public guid Id { get; set; }

        public string Name {get; set;}
    }

    public abstract class Person
    {
        public guid Id { get; set; }

        public string Name { get; set; }

        public guid SchoolId SchoolId { get; set; }

        public School School { get; set; }
    }    

    public class Teacher : Person
    {
        public string TeachersUnionId { get; set; }
    }

    public class Student : Person
    {
        public string StudentUnionId { get; set;}
    }
}

Which in the DB gives us the same structure, a school table with a one 2 many onto the person table but the person table now has a discriminator so that EF can differentiate between a Student and a Teacher.

This is fine and works as expected, the issue I have now is that I want to add a navigation property from the School object onto the Students and Teachers.

We did this in two steps, teachers first, so the school object becomes :

public class School
    {
        public guid Id { get; set; }

        public string Name {get; set;}

        public IList<Teacher> Teachers { get; set; }
    }

This works and allows us to navigate from a school down into a collection of teachers.

Now we want to add the Students Navigation property so we modify the school object to be this

public class School
{
    public guid Id { get; set; }

    public string Name {get; set;}

    public IList<Teacher> Teachers { get; set; }

    public IList<Student> Students { get; set; }
}

But when we apply the migration for this we get

Introducing FOREIGN KEY constraint 'FK_NAME' on table 'Person' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Im normally fine with EF - But Im struggling to get my head around why this isnt working this time - as I said Im sure we have used this structure before.

This is a C# project using .net 5 and Ef Core.

Any ideas?

CodePudding user response:

You can explicitly specify the delete behavior you need by OnDelete(DeleteBehavior.ClientCascade) specifying

builder.HasOne(d => d.Prop1)
            .WithMany(p => p.Prop2)
            .HasForeignKey(d => d.Prop3)
            .OnDelete(DeleteBehavior.ClientCascade)
            .HasConstraintName("FK_Table1_Table2");

DeleteBehavior enum has other options like Cascade,ClientSetNull etc

  • Related