How to determine an object subtype using Inheritance with Entity Framework 4?

I am just starting to use Entity Framework 4 for the first time. While I like it, but I'm a little confused about how to inherit properly.

I am making a model-based approach, and I have a Person object with two subtype objects, Employee and Client. EF correctly uses the table's approach to type, however I cannot figure out how to determine which type of Person is a particular object.

For example, if I do something like

var people = from p in entities.Person select p;
return people.ToList<Person>();

In my list that I build from this, all I care about is the identifier field, so I donโ€™t really want to query all the subtype tables (this is a list of web pages with links, so all I need is a name and Identifier, all in the "Persons" table).

However, I want to create different lists using this one query, one for each type of person (so one list for Customers and another for employees).

The problem is that if I have a Person object, I donโ€™t see a way to determine whether this object is a Client or an Employee without directly querying the client or employee tables. How can I easily determine the subtype of an object without performing a few additional database queries?

+3
source share
1 answer

Use .OfType<Client>()in your request to get only customers. See OfType .

eg. entities.Person.OfType<Client>()...

is, , Person , . if (p is Employee) ...

, entities.People? ?

+5

Source: https://habr.com/ru/post/1745015/


All Articles