efcore icon indicating copy to clipboard operation
efcore copied to clipboard

DeleteBehavior.NoAction Absent?

Open brimmy11 opened this issue 8 years ago • 4 comments

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?

Further technical details

EF Core version: 2.0.2 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Win 10 IDE: Visual Studio 2017 15.4

brimmy11 avatar May 09 '18 04:05 brimmy11

@brimmy11 What is your solution?

pantonis avatar Feb 18 '19 17:02 pantonis

Just curious on what was the solution made to this ? I also ran into this problem.

0x3ch0 avatar Sep 09 '19 06:09 0x3ch0

same problem. I don't want to delete child table records after deleting parent table in mariadb but there is no (NO ACTION) option!

alirezanet avatar Dec 31 '19 12:12 alirezanet

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.

ipatalas avatar Mar 09 '20 10:03 ipatalas