Database Reference
In-Depth Information
12-5. Automatically Deleting Related Entities
Problem
When an entity is deleted, you want to delete the related entities automatically.
Solution
Suppose that you have a table structure that consists of a course, the classes for the course, and the enrollment in each
class, as shown in Figure 12-5 .
Figure 12-5. The Course, Class, and Enrollment tables in our database
Given these tables, you have created a model like the one shown in Figure 12-6 .
Figure 12-6. A model with the Course, Class, and Enrollment entities and their associations
When a course is deleted from the database, you want all of the classes for the course to be deleted and all of the
enrollments for the classes to be deleted as well. To get this to work, we set a cascade delete rule in the database for
the relationships. To set this rule, select the relationship in SQL Server Management Studio, view the properties, and
select Cascade in the INSERT and UPDATE Specification's Delete Rule.
When these tables are imported into the model, these cascade delete rules will also be imported. You can see this
by selecting the one-to-many association between Course and Class and viewing the properties (see Figure 12-7 ).
 
Search WWH ::




Custom Search