TPH is more than a selection!

For some persons, the TPH is just a selection but in fact it’s actually more than this.

Imagine the following table:

  • Id (PK)
  • Type int NULL

Imagine than we want to do TPH on Type column.

I had some questions like is it possible to have an OR. For example Type = 0 OR Type = 1.

The answer is no. Why?

When you use some entities, you probably want to do more than just read it. The SQL generated query for an INSERT include the condition value. So of course you can’t have two values.

However, you can have an entity type for all the non managed cases. When you add a new entity of this type, the Type column will be NULL.

Now what happens if Type is NOT nullable?

In this case, you must define the condition value (or take your entity type abstract). You can define Type = 0 on the base entity type and Type = 1 on the derived one. If you insert a new derived entity type, you will have 1 in your DB. (it’s like virtual override).

EF does a lot of checks to make sure of the EDM coherency with the DB and of the consistency of your EDM. For example, if you have a derived entity with condition Is Null and another with the condition Is Not Null, the base entity type must be abstract.

This entry was posted in 7671, 7674, 7675. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *