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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:27:51+00:00 2026-05-25T02:27:51+00:00

I have a class hierarchy that I want to map across several tables using

  • 0

I have a class hierarchy that I want to map across several tables using Entity Framework 4.1 Code First. It’s like table-per-type (TPT) but I also want a discrimator column.

The hierarchy looks something like:

public class Event
{
    public Guid Id { get; set; }
    public string Code { get; set; } // discriminator
    public DateTime Date { get; set; }
}

public class Party : Event
{
    public int AttendeeCount { get; set; }
}

public class BirthdayParty : Party
{
    public int Age { get; set; }
}

public class WeddingParty : Party
{
    public string Surname { get; set; }
}

This is a pretty weak example but I hope it makes sense. There’ll be an “Events” table, a “Parties” table and a table for each kind of party. However, the discriminator column (“Code”) will have a known value for each kind of event, like “BIRTH” for birthday parties or “WEDDING” for wedding parties.

The idea is that if I query for just birthday parties on a given date, EF would know to add Code = 'BIRTH' to my query instead of doing a bunch of UNIONs and JOINs to work out which rows it needs.

I map my lowest-level classes like this:

var bd = modelBuilder.Entity<BirthdayParty>();
bd.ToTable("BirthdayParties");
bd.Property(p => p.Age).HasColumnName("BirthdayAge");

I now need to specify the discriminator value in there somehow. I’ve tried this:

modelBuilder.Entity<Event>().Map<BirthdayParty>(cfg =>
    {
        cfg.Requires("Code").HasValue("BIRTH");
    });

… but that complains that I haven’t specified the table name inside the call to Map. So I tried moving the ToTable call into there:

var bd = modelBuilder.Entity<BirthdayParty>();
bd.Property(p => p.Age).HasColumnName("BirthdayAge");

modelBuilder.Entity<Event>().Map<BirthdayParty>(cfg =>
    {
        cfg.Requires("Code").HasValue("BIRTH");
        cfg.ToTable("BirthdayParties");
    });

… and now it thinks I want a “Code” column in the “BirthdayParties” table, which is not correct. I’ve already told it that the “Code” column is in the “Events” table.

Is this even possible? Can I combine the use of a discriminator column with a table-per-type mapping?

  • 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-05-25T02:27:53+00:00Added an answer on May 25, 2026 at 2:27 am

    Unfortunately this is not supported. Discriminator column can be used only in TPH. TPT differs entity types by mapped tables and it always produces those terrible queries. It could be nice feature so perhaps suggestion on Data UserVoice would make it implemented one day.

    Update

    There is already a suggestion on user voice for this titled “Discriminator column support in TPT inheritance“.

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

Sidebar

Related Questions

I'm using Entity Framework 4.1's code first approach to map a class hierarchy to
I have an hierarchy of classes that looks like the following: class Critical {
I have a python class hierarchy, that I want to extend at runtime. Furthermore
I have a hierarchy that looks something like this: class Base { public: void
I have a simple Class Hierarchy that I am trying to get to work
I have a small class hierarchy where I would like to have a child
If have a type hierarchy that starts out with a generic type class A<T>
I have a python code like this. File named mymodule.py class MyBase(object): pass File
I have several Entity classes. Now I want to use @PostLoad in every entity.
I have a simple class hierarchy where I have a virtual method that is

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.