I am trying to implement heterogeneous communication in my data model ( Entity Framework 6, Code-First approach ).
I have an existing class structure, let's call them Tree , Branch and Leaf . A Tree can have many Branch objects, and Branch can contain many Leaf objects. The relationships between the three levels have the cascade-delete behavior (delete a branch, and also delete leaves, etc.).
Now I'm trying to allow users to add a comment-like object at each of these levels. I had several problems with data modeling , since I want each of the three types of entities to have many comments and each comment belong to one and only one record. I would also like all comments to be in the same table. I tried two different approaches:
Alt 1
Introduce inheritance so that Comment (abstract) can be TreeComment , BranchComment or LeafComment , following the approach to the hierarchy table (TPH) (as seen, for example, here ) of having an abstract class ( Comment ) for comments, and then output it to TreeComment , BranchComment etc. This is achieved by encoding such models as follows:
public abstract class Comment {
... which can be expressed using this diagram:

The problem with this approach is that the relationship between the comment table and the other 3 does not contain ON DELETE CASCADE or ON DELETE SET NULL . If I try to change this to multiple tables, I get:
Representing the FOREIGN KEY constraint 'FK_Comment_Branch_BranchID' on the Comment table can cause loops or multiple cascading paths. Indicate ON DELETE NO ACTION or DO NOT UPDATE NO ACTION, or change another FOREIGN KEY of restriction.
I understand that this is because SQL Server "does not know" that at any time it is supposed to use only one of the FKs in the comment table.
Alt 2
Summarize the Tree / Branch / Leaf trio in CommentableEntity using the Table for Type (TPT) approach and join the Comment table with this abstract. This can be achieved by implementing inheritance in model classes (as before) and adding the annotations [Table("Tree")] , [Table("Branch")] and [Table("Leaf")] to each of the subclasses so that make sure that we get a table for each (and not one table, as in TPH). The model is as follows:

This approach has two problems:
Deleting a specific object (for example, a branch) will not delete the base record in the abstract table , leaving behind โgarbageโ (abstract entities and their comments).
FK relationships between abstract and concrete classes do not have cascade delete . Therefore, I cannot delete the underlying object. If I try to add it, I get another complaint about how introducing such a rule will cause cycles of several cascading paths.
I also tried to use DB triggers ( CREATE TRIGGER ... INSTEAD OF DELETE... ) for both approaches, but they seem to be big, no, because EF cannot track their changes.
This is disappointing, and I'm sure this (comments on the tree structure) is a very typical scenario in web development; but I cannot find a way to resolve this. I am looking for all the tips I can get about how to effectively model this relationship (EF 6 Code First) without giving too much weight to the Business Logic level.
EDIT:
I believe that this particular user @Deepak Sharma mentioned in his comment: TPH inheritance in node classes. If so, this also does not work for the same reason: loops of multiple cascading paths.
