Luis Abreu
Luis Abreu

Reputation: 4570

EF Core: ignore base class on table inheritance

I'm trying to use the Fluent API to use the TPH for mapping an existing class hierarchy into a single table. In this case, I've got a base class (holds common properties) and then 2 concrete classes that extend the base class with its own properties:

class Base 
{
    // properties
}

class A: Base {}

class B: Base {}

I'm trying to map the classes to the tables by using a custom type configurator and I thought that I could do it like this:

class BaseConfigurator: IEntityTypeConfiguration<Base> 
{
    public void Configure(EntityTypeBuilder<Base> builder)  
    {
       builder.ToTable("...");
       // more mappings
       builder.HasDescriminator(e => e.Type)
              .HasValue<A>("A")
              .HasValue<B>("B");
    }
}

When I try to run the code, I get an error message which says that the base type is part of the hierarchy, but does not have a discriminator value configured.

Since the base type is abstract and it's there only to be save the common properties, how can I say that the base type should be ignored?

Thanks.

Upvotes: 1

Views: 1135

Answers (1)

Luis Abreu
Luis Abreu

Reputation: 4570

Just noticed that I'm missing the abstract qualifier from the base class declaration...adding it seems to solve the problem.

Upvotes: 1

Related Questions