How to: Set Up Cascade Delete for Vertical Inheritance in Visual Designer
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
The ability to set CASCADE delete for table hierarchies through Visual Designer is available only in Visual Studio 2010 and Visual Studio 2012. In Visual Studio 2008 the feature is supported in Code - Only Mapping.
Deleting objects from database tables participating in vertical inheritance, by default, results in a DELETE statement against the table mapped to the base persistent class and multiple DELETE statements against those mapped to derived classes. In cases when the class hierarchy is separated on a lot of levels, this may cause a low performance rate of the application.
Telerik Data Access helps you to solve this issue while you create the domain model by allowing you to set the delete rule of the constraints between the tables to CASCADE. After the database is synchronized with this setting, Telerik Data Access will issue DELETE statements only against the base table and will rely on the backend to delete the related records from the derived tables.
Telerik Data Access offers two ways to control the usage of cascade delete in domain models that have vertical inheritance hierarchies:
The usage of cascade delete varies according to the settings on both model and class level as follows:
Model | Class | Cascade Delete Usage |
Enabled | Default | True |
Enabled | Yes | True |
Enabled | No | False |
Diabled | Default | False |
Diabled | Yes | True |
Diabled | No | False |
Cascade Delete on Domain Model Level
The usage of CASCADE delete for the whole domain model is enabled and disabled through the Use Cascade Delete for table hierarchies option (Backend Configuration Settings / Runtime in Model Settings).
The workflow for enabling it includes the next steps:
-
Check the Use Cascade Delete for table hierarchies check-box in the Runtime tab in the Model Settings dialogue, Backend Configuration Settings page
Click OK to close the dialogue and save the domain model.
-
With the help of the Update Database from Model wizard, create/migrate the domain model to the database. If you look through the script this method produces, you will notice that all of the constraints between the tables which form the entity are (re)created to allow cascade delete. For example:
ALTER TABLE [Cat] ADD CONSTRAINT [ref_Cat_Animal] FOREIGN KEY ([AnimalId]) REFERENCES [Animal] ([AnimalId]) ON DELETE CASCADE go ALTER TABLE [Dog] ADD CONSTRAINT [ref_Dog_Animal] FOREIGN KEY ([AnimalId]) REFERENCES [Animal] ([AnimalId]) ON DELETE CASCADE go ALTER TABLE [WienerDog] ADD CONSTRAINT [ref_WienerDog_Dog] FOREIGN KEY ([AnimalId]) REFERENCES [Dog] ([AnimalId]) ON DELETE CASCADE go
-
Make sure to select the Create Script File and Execute option on the Summary page of the Update Database from Model wizard.
-
If you attempt to delete the first Animal object, you will notice that Telerik Data Access issues a DELETE statement only against the Animal table:
using (FluentModel dbContext = new FluentModel()) { Animal firstAnimal = dbContext.Animals.FirstOrDefault(); dbContext.Delete(firstAnimal); dbContext.SaveChanges(); }
Using dbContext As New FluentModel() Dim firstAnimal As Animal = dbContext.Animals.FirstOrDefault() dbContext.Delete(firstAnimal) dbContext.SaveChanges() End Using
The generated SQL statement would be:
DELETE FROM [Animal] WHERE [AnimalId] = @p0
The state of Use Cascade Delete for table hierarchies will influence the constraints between the tables that build the vertical inheritance hierarchy only. Additionally, if a base class is explicitly set to use (or not use) cascade delete on class level, Telerik Data Access will disregard the state of Use Cascade Delete for table hierarchies and takes into account the class' setting when it generates DELETE statements against the tables mapped to its derived classes.
Cascade Delete on Class Level
Since a domain model can contain a variety of entities connected with one another in different ways, you may need to control the usage of CASCADE delete only for a particular base classes that participate in different vertical inheritance hierarchies.
The workflow for enabling it includes the following steps:
- Select the base class in Visual Designer and press F4 to open the Properties Window.
-
Set the Use Cascade Delete property to True and save the domain model
-
With the help of the Update Database from Model wizard, create/migrate the domain model to the database. If you look through the script this method produces, you will notice that all of the constraints between the tables which form the entity are (re)created to allow cascade delete. For example:
ALTER TABLE [Dog] ADD CONSTRAINT [ref_Dog_Animal] FOREIGN KEY ([AnimalId]) REFERENCES [Animal] ([AnimalId]) ON DELETE CASCADE go
You can test the removal of objects from the hierarchy as shown here.
Setting Use Cascade Delete to No will explicitly disable the usage of cascade delete for a particular base class disregarding the state of Use Cascade Delete for table hierarchies. Use Cascade Delete equal to Default will respect the value of the Use Cascade Delete for table hierarchies check-box of the runtime configuration.