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);