We’re using L2S and we have a class like this:
public class HumanContainer
{
public List<IHuman> Humans { get; set; }
public string SomeOtherProperty { get; set; }
}
Our database has tables like this:
HumanContainer
– Geek
We’ve only had one type of Human so far (Geek). And when we send/retrieve HumanContainers to/from the DB, we know to treat them as Geeks. Now that we need a second Human (Athlete), we have a choice to make for how to implement this.
One option is to create another table (Athlete) in the DB:
HumanContainer
– Geek
– Athlete
For every new concrete Human like this, we’ll need to loop through HumanContainer.Humans, detect the type, add it to the appropriate EntitySet<>, then save.
Another option is to have only one table for all Humans:
HumanContainer
– Humans
If we do that, then we’ll need something like an XML column where we serialize the Human into its specific type and store it in that column. Then we’ll need to deserialize that column when retrieving the data.
Is one of the approaches recommended? I’m curious to know how people have been handling this situation. Is there a third approach that I haven’t listed here?
What it sounds like you are trying to do is represent inheritance in a relational database. Guy Burstein has a pair of great articles on this subject: How To: Model Inheritance in Databases and Linq to SQL Inheritance.