Can you use class inheritance (classes derived from a base)?
I realize this may stray from the "Plain" in POCO, but I have multiple classes that are variations of an object and share a lot of common fields. The common fields saved in one table, and a table for each derived class's unique fields. Using Animals as a fake example...
[TableName("Animals")]
[PrimaryKey("Id")]
public class Animal
{
public int Id { get; set; }
public string Name { get; set; }
}
[TableName("Cats")]
[PrimaryKey("AnimalId", AutoIncrement = false)]
public class Cat : Animal
{
public int AnimalId { get; set; }
public string FurColor{ get; set; }
}
[TableName("Fishes")]
[PrimaryKey("AnimalId", AutoIncrement = false)]
public class Fish : Animal
{
public int AnimalId { get; set; }
public string FinShape { get; set; }
}
var cat = new Cat {
Id = 1,
Name = "Stripes",
FurColor = "Tabby",
}
// SELECT * FROM Animals A JOIN Cats C ON A.Id = C.AnimalId
There are places where I need a list of all animals, so having fully separate classes would mean doing UNIONs to get the full set, and the IDs would collide or need to be shaped into composite keys. Alternately having one table with all of the fields for all kinds of animal seems bad, with large sets of nulls fields in each row and many indexes for searching different kinds on animals.
First is this a bad idea, and if so what would be better?
If this can be done does it just magically work or does it need additional steps? For example, insert the Animal and insert the Cat, or would inserting just the Cat also create the Animal record?