Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8460515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:37:46+00:00 2026-06-10T13:37:46+00:00

Consider the following entity framework model with TPT inheritance. DB Tables: Person (PersonID, Name)

  • 0

Consider the following entity framework model with TPT inheritance.

DB Tables:

Person (PersonID, Name)
Student (PersonID, Grade)

EF Entities:

Person (PersonID, Name)
Student (Grade) : inherits from Person

Now when you’re trying to select a person entry from the database it will return Student type instead.

var person = db.Persons.First();
// person here is of type Student and has Grade peoperty populated
// SQL query generated by EF selects data from both tables with a JOIN

How to force this query select only the data from Person db table and not from both Person and Student db tables?

For example, it can be done with the following query:

db.Persons.Select(x => new Person { PersonID = x.PersonID, Name = x.Name }).First() 

but it looks lame, generates an extra SELECT statement over the existing query and this way returned Person entity object will not be tracked by EF context. So, I’m wondering why db.Persons.First() returns a Student object? Isn’t it counterintuitive?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:37:48+00:00Added an answer on June 10, 2026 at 1:37 pm

    If you use a list of Person and stores a single Student to the list what will you receive when you call First? You will receive a Student instance because that is how object oriented code is supposed to work Student is a Person but you will never get just Person instance without creating a new instance and copying data from original Student instance.

    EF works in the same way – entity is atomic. It doesn’t matter how many tables it spans. If you query inheritance hierarchy you will always get the whole instance of the correct type because that is how object oriented code is supposed to work.

    Your second example should not work at all if used in Linq-to-entities because you are creating instance of the entity inside the query – that is not allowed. Projections must not be done to mapped entities because it could break data consistency.

    The way to go is using projection either to non entity type – custom not mapped class or anonymous type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following hibernate configuration: <class name=Person> <id name=id column=personId> <generator class=native/> </id> <set
Please consider the following two entities: @Entity @Table(name = PROFILE) public class Profile extends
Consider the following code: @Entity @Table(name = a) public class A implements Serializable {
Consider that i have two entities with following relationship: Entity A <-->> Entity B
Consider the following: NSFetchRequest *request = [[NSFetchRequest Alloc] init]; request.entity = [NSEntityDescription entityWithName:@Person inContext:_MOC];
Consider the following entity model: public class Agreement : Entity { public int AgreementId
Consider the following two relations: @Entity class Foo { @Id id; @ManyToMany @JoinTable(name =
This might be a entity framework related question, anyway, here goes: Consider the following
I am using DBContext API from EF 4.1. Consider following entity model (A, B,
Consider the following code var q = from e in myCollection.AsQueryable<Entity>() where e.Name ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.