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 8269391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:12:39+00:00 2026-06-08T06:12:39+00:00

Consider following case: I have base class inherited by another two. For instance we

  • 0

Consider following case:

I have base class inherited by another two.
For instance we will have:

class Base
{

}
class DerivedA : Base
{

}
class DerivedB : Base
{

}

When I request base entity with code like

context.Base.Where(q => q.Id == id).First();

Entity Framework generate a full set of joins to every derived entity which results in below acceptable perfomance of query.
What I want is to load only base entity without joining it to derived ones.

The only solution I found described here http://blogs.msdn.com/b/alexj/archive/2009/09/17/tip-35-how-to-write-oftypeonly-tentity.aspx.
But that did not work for me. EF still generates huge query.

Writing queries like:

context.Base.Where(q => !(q is DerivedA) && !(q is DerivedB)).First();

doesn’t suits me either due to constantly increasing amount of derived types.

Are there any possible solutions apart from those I mentioned?

  • 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-08T06:12:40+00:00Added an answer on June 8, 2026 at 6:12 am

    You are using Table-per-type => EF must generate those joins because it doesn’t know what type your entity really is. If your record from Base has a related record in DerivedA table IT MUST NOT create instance of Base entity for that record – IT MUST create instance of DerivedA and it needs to make joins to get this knowledge.

    It is for a long discussion why this is not allowed but simply entity is from object world – it is atomic data structure which can be saved to multiple tables but that is invisible to object world. If you persist a Base you will load back a Base but if you persist DerivedA you will always load back DerivedA and never just Base because it would break the atomicity of the entity.

    I didn’t try it but I assume that ESQL OFTYPE ONLY operator should do joins as well to make sure that it really loads real instances of the base entity instead of corrupted instances of derived entities.

    Table-per-type produces slow queries. .NET 4.5 should improve it but I think improvements will not target these cases – I think they will target OfType for derived types and projections.

    If you want only data from Base table your best options are:

    • Hand written SQL – projections in .NET 4.0 still do some unnecessary joins or unions
    • .NET 4.5 and projection in Linq
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following case: public class A { public A() { b = new
Please consider the following simple use case: public class Foo { public virtual int
Consider the following security problem: I have a static base path ( /home/username/ )
Consider the following Spring Service class. The spring scope defined is Singleton. The two
Consider the following case: I have a single Excel workbook with 4 sheets in
Having the SOLID principles and testability in mind, consider the following case: You have
Consider the (highly simplified) following case: class Dispatcher { public: receive() {/*implementation*/}; // callback
Today, I've stumbled over the following: Consider two classes NewClass and NewClass1, which have
Consider the following: I have two tables (Customers and Cases) defined as follows: **Customers**
I have a relatively simple use case which is failing. Consider the following code:

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.