I'm trying to assign a DbSet
of class that is inherited from a more generic class. The example below demonstrates what I'm trying to achieve:
DbSet<Animal> dbSet;
switch (selectedAnimal)
{
case "Dog":
dbSet = _Context.Dog; // DbSet<Dog>
break;
case "Cat":
dbSet = _Context.Cat; // DbSet<Cat>
break;
case "Pig":
dbSet = _Context.Pig; // DbSet<Pig>
break;
}
The Dog
, Cat
, Pig
class is an inherited class of Animal
, as follows:
public class Dog : Animal { }
But I am getting a compile-time error of
Cannot implicitly convert type DbSet<Dog> to DbSet<Animal>
How can I implement my design without getting the error?
P.S. The code above is just to illustrate what I'm trying to achieve, so forgive me if it doesn't make sense.