Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade delete in table per type(TPT) #24622

Closed
dimitkos opened this issue Apr 9, 2021 · 2 comments
Closed

Cascade delete in table per type(TPT) #24622

dimitkos opened this issue Apr 9, 2021 · 2 comments

Comments

@dimitkos
Copy link

dimitkos commented Apr 9, 2021

EF Core version: 5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows10
IDE: Visual Studio 2019 16.8.6

I have two entities employee, manager, which class manager inherit from the employee

public class Employee
{
        public long Id{ get; private set; }
        public string FirstName { get; private set; }
        public string LastName { get; private set; }
}
public class Manager: Employee
{
        public long StoreId{ get; private set; }
}

I am using type per table due to parent-child relationship, with fluent API configuration

class EmployeeConfiguration : IEntityTypeConfiguration<Employee>
    {

        public void Configure(EntityTypeBuilder<Employee> builder)
        {
            builder.ToTable("Employee");
            builder.HasKey(x => x.Id);    

        }
    }

class ManagerConfiguration : IEntityTypeConfiguration<Manager>
    {

        public void Configure(EntityTypeBuilder<Manager> builder)
        {
            builder.ToTable("Manager");
        }
    }

As a result, I have two tables in the SQL server.
The problem is, I want a cascade on delete behavior, when I remove the employee, I want to remove and the record of the manager table. But the default behavior is not this. When I try to delete the parent record I have an FK violation.

I try this method and get the error

public async Task DeleteManagers(long[ ] managerIds)
{
      var sqlCommand = $"DELETE FROM dbo.Manager WHERE Id IN ({string.Join(',', managerIds)})";
      await _context.Database.ExecuteSqlRawAsync(sqlCommand);
}

Also in the migrations file, the autogenerated code from migration is

b.HasOne(".......Employee", null)
                        .WithOne()
                        .HasForeignKey("....Manager", "Id")
                        .OnDelete(DeleteBehavior.ClientCascade)
                        .IsRequired();

How I can configure the OnDelete to cascade in a table per type case?

@ajcvickers
Copy link
Member

Note for triage: confirmed we don't configure cascade delete here.

@AndriySvyryd
Copy link
Member

This is by design, using CASCADE by default will very likely result in an error:

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

However you can configure it manually:

modelBuilder.Entity<Derived>().HasOne<Base>().WithOne()
    .HasForeignKey<Derived>(d => d.Id).OnDelete(DeleteBehavior.Cascade);

@AndriySvyryd AndriySvyryd removed their assignment Aug 24, 2021
@AndriySvyryd AndriySvyryd removed this from the 6.0.0 milestone Aug 24, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants