Search code examples
c#entity-frameworklinq-to-entitiestable-per-type

Prevent the loading of subclass table rows in TPT inheritance on Entity Framework


I have the following table hierarchy in my database which i map to an Entity Framework model (Database First) using the Table-Per-Type (TPT) inheritance pattern:

TPT inheritance

The mapping in the EF model is straightforward: AssetContent is a base abstract class while the other 2 are concrete subclasses.

The AssetContent table participates into a many-to-many relationship with another table which, to keep the picture clear, is omitted.

My question is, how do i build a Linq-to-Entities query to load the related AssetContent table using Include() such that the 2 'sub-tables' are not loaded at all? This is especially important for the DatabaseAssetContent table, whose BinaryContent field may be quite large and of no relevance to the issuer of the query i want to build. As far as i observed, Entity Framework automatically loads the entire hierarchy for a table, whether lazy loading is enabled or not, but i am interested in loading only the rows in the AssetContent table.

Is such a query possible using Linq-to-Entities (for Entity Framework 6)?


Solution

  • Eventually, i moved the AssetContent table's fields (except the Id) into another, new table, called AssetContentWithMetadata, which has a 1-1 relationship to the AssetContent table. This way, the AssetContent table remains indeed a bit awkward, with a single field (the ID), but now i can load the metadata table alone, without burdening it with the contents as well.