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.
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?
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.
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:
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)
I check Cascade Delete ON in RBS
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
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.