Disable Cascading delete not working

Hello,

If I disable Cascading delete in Code generation settings Cascading delete still is used:

image

I cannot see any difference if it is disabled or enabled in generated code:

public async Task<PDBIntern.Models.PDB.Bezirk> DeleteBezirk(int bezirkid)
        {
            var itemToDelete = Context.Bezirks
                              .Where(i => i.Bezirk_ID == bezirkid)
                              .FirstOrDefault();

            if (itemToDelete == null)
            {
               throw new Exception("Item no longer available");
            }

            OnBezirkDeleted(itemToDelete);


            Context.Bezirks.Remove(itemToDelete);

            try
            {
                Context.SaveChanges();
            }
            catch
            {
                Context.Entry(itemToDelete).State = EntityState.Unchanged;
                throw;
            }

            OnAfterBezirkDeleted(itemToDelete);

            return itemToDelete;
        }

robert

I think this is done in your DbContext.

Check your

protected override void OnModelCreating(ModelBuilder builder)
{
...
}

In the definition of you entities could be something like this for disabled cascading:
.OnDelete(DeleteBehavior.Restrict)

No, there is no difference if it is enabled/disabled and no OnDelete(DeleteBehavior.Restrict)

maybe a Bug in Balzor Studio?

robert

I'm seeing the same thing as far as scaffolding with or without the cascade delete flag set does not change the code at all.

When cascade delete is enabled the Delete method in the service in Blazor server app and the ODataController in the WebAssembly app will have Include() for related entities:

I double checked our template and I've noticed that there are cases where the code will not be generated - the problem is fixed immediately and the fix will be part of our next update today/tomorrow.

1 Like

Just FYI, confirmed it now works as expected in 1.8.0

Could you please add to the list of enhancements to save the prior setting?

I don't want cascade delete and am re-scaffolding often, every time I need to uncheck it rather that it remembering the setting from the last run.

Same for the CRUD configuration, would be great if remembered those values from the prior run.

Using MS SQL, RBS with .net 8.0, I have the following behavior:

  1. There is no cascade delete in SQL server (if I try to execute DELETE FROM Parent WHERE ParentID = x and ParentID x has children, it fails)
  2. I checked off Cascade Delete in RBS
  3. I did verify that there is no Include in the DeleteParent code generated by RBS
  4. Yet I am able to delete a Parent with Children from the RBS application....

Please check it...

Hi @semafox,

Try to add the following code in your DbContext ConfigureConventions method:

configurationBuilder.Conventions.Remove(typeof(Microsoft.EntityFrameworkCore.Metadata.Conventions.CascadeDeleteConvention));
configurationBuilder.Conventions.Remove(typeof(Microsoft.EntityFrameworkCore.Metadata.Conventions.SqlServerOnDeleteConvention));

So that works but it also creates a problem. My parent table is QUOTE and it has 3 children QUOTELINE, QUOTESTATUS and QUOTEDOCUMENT. Each of those tables have records. When I try to delete a QUOTE record from my RBS app, sometimes it works and sometimes I get the following error message:

'The association between entity types 'Quote' and 'QuoteDocument' has been severed, but the relationship is either marked as required or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, configure the relationship to use cascade deletes. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.'

Of, course that's because of the change of setting, but CasCade delete is implemented as the SQL Server level and I was hoping it would work this way...

It would give me great flexibility if I could just define Relational Integrity at the SQL Server level and then have my Blazor app just work with it. This way of doing things was working well with the now defunct LightSwitch... But I realize that may be a .Net Core limitation. Would there be any way to make this work?

Thanks

Well, when cascade delete is disabled you will unable to delete records if the constraint does not allow it. Not sure what else we can do.

What I was hoping to achieve and which used to work very well with Microsoft LightSwitch is that the data layer would simply respect the Relational Integrity rules set in the SQL Server database. The problem with the Radzen setup is that it's all or nothing: either all delete cascade or none, when in reality different relationships have different rules. Being able to configure the relational integrity rule of each relationship at the SQL Server layer and then knowing that Radzen would respect those would be an awesome RAD feature.

1 Like

That’s not true. The Include() only solution is exactly this - will cascade depending on database settings.

If your relations are optional (nullable foreign key) and you delete parent entity all child entities foreign keys will be set to null. If your relations are required loaded in memory child entities will be deleted. In case of required relation you will unable to delete parent record if related records are not included.

You mean to say that if I turn Cascade Delete on in RBS then the application will respect the RI database settings?

But I found this not to be true. Check the following scenario:

  1. There is no cascade delete in SQL server (if I try to execute DELETE FROM Parent WHERE ParentID = x and ParentID x has children, it fails)
  2. I check Cascade Delete ON in RBS
  3. Yet I am able to delete the Parent record... which means that my RBS app is not respecting the database rules...
    So in that case the Cascade Delete in RBS overrides the absence of Cascade Delete in SQL

What am I missing?

Please check it...

I tested a similar setup and ended up with this exception when I tried to delete a record that has children:

fail: Microsoft.EntityFrameworkCore.Update[10000]
      An exception occurred in the database while saving changes for context type 'Net8Wasm.Server.Data.RadzenSampleContext'.
      Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
       ---> Microsoft.Data.SqlClient.SqlException (0x80131904): The DELETE statement conflicted with the REFERENCE constraint "OrderDetails_FK". The conflict occurred in database "RadzenSample", table "dbo.OrderDetails", column 'OrderId'.

Then when I enabled cascade deletes in the database they cascaded as expected. So I see the behavior that you request when the "Cascade delete" setting in Radzen Blazor Studio is unchecked.

Probably there is something specific to your database schema that we are missing. Please send SQL CREATE script to info@radzen.com.