Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding inherited object by ID - Entity Framework

As you can see in the following image, I have a model with a base class "Person" and both entities "Kunde" and "Techniker" inherit the base class.

Now I've got following problem. When I try to use the method Find to get an object of the derived class Kunde with given ID, it tells me that OfType<TResult> is a method and isn't valid in this context.

public Kunde GetById(int id)
{
   return dbModel.PersonMenge.OfType<Kunde>.Find(id);
}

I've also tried to drop the OfType but it obviously tells me that the object Person cannot be implicitly converted to Kunde.

Is there anything I'm missing here?

like image 479
HansWurs Avatar asked Nov 13 '22 16:11

HansWurs


1 Answers

It's just two parentheses:

OfType<Kunde>()

But then you can't use Find any more, because that is a method of DbSet. You have to use Single, or dbModel.PersonMenge.Find(id) as Kunde;.

like image 181
Gert Arnold Avatar answered Nov 15 '22 07:11

Gert Arnold