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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:19:41+00:00 2026-06-15T13:19:41+00:00

Overview I am using entity framework 4.3 code first with the fluent interface to

  • 0

Overview

I am using entity framework 4.3 code first with the fluent interface to setup my DbContext. I have a base Item class with other types that inherit this such as an Event, BlogPost, ForumThread, WikiPage and so on.

These inherited types are mapped with what I think entity framework refers to as TPT inheritance. This works great when querying a single type like ‘events’ or ‘blog posts’ but constructs very complicated queries with horrible performance when trying to query across all types due to the joins required in order to achieve the polymorphic behaviour EF provides out of the box.

Problem Context

I am wanting to build a global search feature where I only need access to the base ‘Item’ entity and not the inherited instances. I’d like to be able to query across the base item class by name, tags and so on. Performing any sort of LINQ query, even when requesting the base item type still results in polymorphic behaviour which kills performance.

Code First Model

public class Item
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Body { get; set; }

    public DateTime Created { get; set; }

    public int? CreatedBy { get; set; }

    public int? LastModifiedBy { get; set; }

    public DateTime? LastModified { get; set; }

    public virtual User Author { get; set; }

    public bool IsDeleted { get; set; }

    public string ImageUri { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }
}

public class Event : Item
{
    // Additional properties
}

public class BlogPost : Item
{
    // Additional properties
}

What I would like to be able to do is map another POCO to the same base table so that when I construct queries on it, it doesn’t involve the inheritence issues. EF doesn’t seem to like this though. I don’t have the error on hand at the moment but my attempts at a simple mapping failed.

Alternate Solutions?

  • I had thought about implementing an ‘index’ table which would look similar to the ‘Item’ table and inserting a record into that whenever a new item type is created. But this index data would then also need to be updated whenever the event, blog post data changes etc. This is further complicated by foreign keys such as tags. Whenever tags on say an event are changed I would have to make sure these changes are synchronised on the matching index table too. When considering all the different item types, this would become a bit of a nightmare to manage and frankly, doesn’t seem like a very elegant solution.

  • Database triggers

My preferred solution here would be one that is in code and not database triggers / stored procs.

Is there a way to construct a query to force EF to only return the base type instead of the polymorphic type which results in too many joins and horrible performance? Or is there some other clever way around this?

Update

After updating to EntityFramework 5 via Nuget (targeting .Net 4.0) I have been able to query items by their tags and project into a new SearchItem which results in fairly clean SQL without joins to the TPT types.

        var x = from item in repository.FindAll<Item>()
                where item.Tags.Any(t => t.Name == "test")
                select new SearchItem
                {
                    Id = item.Id,
                    Name = item.Name,
                    Body = item.Body,
                    Created = item.Created,
                    CreatedBy = item.CreatedBy,
                    IsDeleted = item.IsDeleted,
                    ImageUri = item.ImageUri,
                    MembershipEntityId = item.MembershipEntityId,
                    //Tags = (from t in item.Tags
                    //       select new Tag
                    //       {
                    //           Id = t.Id,
                    //           Name = t.Name,
                    //           MembershipEntityId = t.MembershipEntityId
                    //       })
                };

SQL

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name], 
[Extent1].[Body] AS [Body], 
[Extent1].[Created] AS [Created], 
[Extent1].[CreatedBy] AS [CreatedBy], 
[Extent1].[IsDeleted] AS [IsDeleted], 
[Extent1].[ImageUri] AS [ImageUri], 
[Extent1].[MembershipEntityId] AS [MembershipEntityId]
FROM [dbo].[Item] AS [Extent1]
WHERE  EXISTS (SELECT 
1 AS [C1]
FROM  [dbo].[ItemTag] AS [Extent2]
INNER JOIN [dbo].[Tag] AS [Extent3] ON [Extent3].[Id] = [Extent2].[Tag_Id]
WHERE ([Extent1].[Id] = [Extent2].[Item_Id]) AND (N'test' = [Extent3].[Name])
)

This has solved half of my problem as I can now search across the base type by tag. I would however like to be able to return the tags with the new projection. Including that commented out bit of code results in a query that EF cannot translate though. Is there a workaround for this?

  • 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-15T13:19:43+00:00Added an answer on June 15, 2026 at 1:19 pm

    Is there a way to construct a query to force EF to only return the
    base type instead of the polymorphic type which results in too many
    joins and horrible performance?

    Generally no. You have mapped inheritance and if you want to return instances of Item, EF must always return correct type => it needs those joins. EF also doesn’t allow mapping the same table multiple times so you cannot have in the same mapping the Item mapped again as another POCO.

    In theory you should be able to query Items and project to your non mapped POCO class only properties you want from the base class. Unfortunately this didn’t work in .NET 4.0 – EF still performed joins. You can try this with .NET 4.5 and EF 5.0 where this issue should be solved.

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

Sidebar

Related Questions

I'm using the jQuery validate plugin. I have this code: $(.btn-overview-basic-save).click(function(e) { if (!$('.contact-overview').valid())
Problem Overview I'm using apache camel 2.4 and have been asked to setup an
Overview: I have an advancedDataGrid that I am using a GroupingCollection on and I
I'm using URL Fetch Java API http://code.google.com/appengine/docs/java/urlfetch/overview.html#Fet ... to get information from graph.facebook.com, however,
Im using mono(3.0.3) on a raspberry pi. Everything is working properly except entity framework.
I am working on web application and using GAE/J blobstore tutorial http://code.google.com/appengine/docs/java/blobstore/overview.html I was
So, I just got setup using Rails 3, Devise and OmniAuth via https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview .
In Eclipse we are using Mylyn to have an overview of our Trac Tickets.
I am using Jquery. I have got below links in my html <a class=load-fragment
I'm very new to this Entity Framework Object Services Overview (Entity Framework) , so

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.