简体   繁体   中英

Entity Framework TPT Inheritance Subtypes

How can i determine the subtype of an entity with a TPT-Inhertiance?

If i have a base class Person and two subclasses Manager and Customer, it should be possible to query all persons and then group by their subclasses through using the GetType-Method, yet the returned type is always person. Eg:

var persons = ctx.Persons.ToList();

var managers = persons.Where(x => x.GetType() == typeof(Manager)).ToList();

Select:

var managers = ctx.Persons.OfType<Manager>().ToList();

also useful if you don't know what you have got

var persons = ctx.Persons.ToList();
Type modelType = persons.First().GetType();
if (modelType.BaseType == typeof(Manager))
{
    ((Manager)persons.First()).GiveNeilAPayRise = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM