Delete Rule: NoAction not applying

Posts   
 
    
Harry H
User
Posts: 2
Joined: 26-Nov-2024
# Posted on: 27-Nov-2024 03:34:45   

Environment: LLBLGen: 5.11.3 09-Oct-2024. SQL Server Express v15. EF Core 8 and C#. Default code generation templates

Hi,

I have a 2x 1:many relationships in the ChannelLink entity where the 2x foreign keys link to a single entity (Channel). When generating the SQL Server DB from the C# code, I get an errors saying that there cannot be multiple cascading deletes:

Microsoft.Data.SqlClient.SqlException: 'Introducing FOREIGN KEY constraint 'FK_ChannelLink_Channel_TargetChannelId' on table 'ChannelLink' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Hence I wish to turn I want to specify one relationship's Delete Rule as 'NoAction'. What I am finding in the LLBLGen .NET 8 code generation is the following:

protected virtual void MapChannelLink(EntityTypeBuilder<ChannelLink> config)
{
    config.ToTable("ChannelLink", "Models");
    config.HasKey(t => t.Id);
    config.Property(t => t.ConnectionLinkType).HasMaxLength(50);
    config.Property(t => t.DataLinkType).HasMaxLength(50);
    config.Property(t => t.Id);
    config.Property(t => t.SourceChannelId);
    config.Property(t => t.TargetChannelId);
    config.HasOne(t => t.SourceChannel).WithMany(t => t.SourceChannelLinks).HasForeignKey(t => t.SourceChannelId);
    config.HasOne(t => t.TargetChannel).WithMany(t => t.TargetChannelLinks).HasForeignKey(t => t.TargetChannelId).OnDelete(DeleteBehavior.Cascade);
}

I have configured the SourceChannel Relationship to be Delete Rule=NoAction, and the TargetChannel relationship to be Delete Rule=Cascade. However with this code, when generating the DB from the C# Code I still get the same error (note that when generating the database from SQL Script it works OK). What I have noticed is that there is no 'OnDelete' specified for SourceChannel relationship line in the code, therefore I believe the default DeleteBehavior is being used: https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.deletebehavior?view=efcore-8.0 Based on the documentation, the cascade DeleteBehaviour is used by default for a required relationship. The default behaviour changes depending on the type of relationship.

Is this a bug in the code generation that .OnDelete(DeleteBehavior.NoAction) is missing on the Source Channel Line? I.e.

config.HasOne(t => t.SourceChannel).WithMany(t => t.SourceChannelLinks).HasForeignKey(t => t.SourceChannelId).OnDelete(DeleteBehavior.NoAction);
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 27-Nov-2024 09:52:54   

Checking the sourcecode, I see we didn't adjust this after Microsoft extended the enumeration with more than just Cascade, Restrict and SetNull.

I think SetNull could also help in your case, but we'll fix it in the templates as well.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 27-Nov-2024 11:58:41   

Hotfix builds 5.10.6 and 5.11.4 contain the fix for this (now available) simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Harry H
User
Posts: 2
Joined: 26-Nov-2024
# Posted on: 05-Dec-2024 05:23:18   

Thank you for such a quick turnaround, I haven't had a chance to try the new version but I will mark this one as resolved simple_smile