Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I specify a discriminator column with a table-per-type mapping?

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?

like image 318
Matt Hamilton Avatar asked Aug 22 '11 00:08

Matt Hamilton


People also ask

How do I add a Discriminator column to a table?

By default, the discriminator column is added to the table with the name “Discriminator” and the CLR type name of each type in the hierarchy is used for the discriminator values. You can modify the default behavior by using the fluent API. In the TPT mapping scenario, all types are mapped to individual tables.

Do I need a discriminator in OpenJPA hierarchies?

JOINED hierarchies can use a discriminator to make some operations more efficient, but do not require one. TABLE_PER_CLASS hierarchies have no use for a discriminator. OpenJPA defines additional discriminator strategies; see Section 7, “ Additional JPA Mappings ” in the Reference Guide for details.

What is the default name of the Discriminator column in SQL?

By default, the discriminator column is added to the table with the name “Discriminator” and the CLR type name of each type in the hierarchy is used for the discriminator values. You can modify the default behavior by using the fluent API.

Which discriminator type should I use?

Like any column that needs to be indexed, the discriminator type is very important to application performance, and you should always choose the most compact type available. Although the default STRING DiscriminatorType is very convenient, it’s much better to use an INTEGER discriminator type.


1 Answers

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".

like image 191
Ladislav Mrnka Avatar answered Sep 17 '22 15:09

Ladislav Mrnka