views:

22

answers:

1

Suppose I have

table Person
table Employee, which inherits Person.

I want to get a list of Person, regardless if this Person is Employee or not. How do you get entity framework to do that without joining the Employee table? C# is what I'm using. Thanks.

A: 

You need to make sure that you don't implicitly use any information from Employee.

If you do this:

var people = Context.People.ToList();

... Then me Entity Framework will initialize new instances of type Employee for those people who happen to be employees. This is because when you store an Employee in the database, you generally expect to get an Employee back when you select the same item.

Nor, for that matter, can you tell the Entity Framework to give you a Person when the stored value is an Employee. The Entity Framework will never give you back an incorrect entity type.

However, if you do just want to read the one table, there is a way to do that: Select the data from Person into a non-entity types, such as an anonymous type:

var people = Context.People.Select(p => new { Name = p.Name }).ToList();
Craig Stuntz