I have a hierarchical database structure with a base table and then several child tables:
Base: {ID, Date, ...}
ChildA: {ID, Color, ...}
ChildB: {ID, Age, ...}
Each child table has an ID that is a foreign key to Base.ID; so each child is linked to an ID that is also in Base.
I now have a situation in which I have a list of IDs and I want to figure out which child tables they actually belong to. What is the best way to determine the child table of a given ID?
I’d like to have a function that returns a Type from an ID: Type TypeFromBaseID(int baseID)
I can think of two ways to do this, but I’m hoping there is a better way:
A) Simply add a column to Base that stores the table name of its child table
B) Have a series of if statements that does something like db.ChildA.Any(x=>x.ID == baseID)
What you proposed in A) is effectively what is known in ER modeling as “discriminator” and this seems like a cleaner solution to me.
I would just urge you to reconsider using full table names here. Try saving some space (and performance) by using integers (and clearly documenting which integer value “maps” to which sub-table).