You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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
I am using type per table due to parent-child relationship, with fluent API configuration
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
Also in the migrations file, the autogenerated code from migration is
How I can configure the OnDelete to cascade in a table per type case?
The text was updated successfully, but these errors were encountered: