Table per class hierarchy requires nullable columns for derived classes
When persisting class hierarchies, all properties in derived classes must be nullable.
Problem
A common pattern for representing class hierarchies in relational databases is to use a table that will have columns for each of the base and derived classes. For example, imagine we have a Vehicle
base class and two derived classes, LandVehicle
and AirVehicle
:
Using the Table per class hierarchy (also called single table inheritance), this could be represented as follows:
You may have noticed that the columns that correspond to the properties in the derived classes–Wheels
and Wings
–are set to accept nulls. Why is that? Well, since this table has to support a schema for all the possible derived classes, we never know, for each row, which class it will map to. For that reason, all columns that correspond to the properties in derived classes need to be nullable...