I am trying to set my foreign key on-delete action to no action globally. I want it so that you cannot delete a parent entry if there are children links, I do not want cascade deleting.
I found an example online that showed you could do it in your OnModelCreating as below
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
When generating a migration I then get
onDelete: ReferentialAction.Restrict
When updating the database I am then getting an error
may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
If I dig in to the ReferentialAction enum I can see there is a NoAction value, which I believe is what I need.
namespace Microsoft.EntityFrameworkCore.Migrations
{
public enum ReferentialAction
{
NoAction = 0,
Restrict = 1,
Cascade = 2,
SetNull = 3,
SetDefault = 4
}
}
And if I look back at the DeleteBehavior enum that was set in the OnModelCreating I can see there is no NoAction value
namespace Microsoft.EntityFrameworkCore
{
public enum DeleteBehavior
{
ClientSetNull = 0,
Restrict = 1,
SetNull = 2,
Cascade = 3
}
}
What am I missing here?
EF Core version: 2.0.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win 10
IDE: Visual Studio 2017 15.4
@brimmy11 What is your solution?
Just curious on what was the solution made to this ? I also ran into this problem.
same problem. I don't want to delete child table records after deleting parent table in mariadb but there is no (NO ACTION) option!
For future people coming here to find out there is no solution here:
That has been added in EF Core 3.0 and newer. Found it here: https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.deletebehavior?view=efcore-3.1
Switch version to see how it changes :)
I have same issue in my project. My workaround was to just alter pregenerated migration.
It's using different enum (ReferentialAction) which already contains NoAction value. Not the best solution but works just fine.
Most helpful comment
@brimmy11 What is your solution?