Configuring relationships between 2 TPH types using Fluent API

92 views Asked by At

I have 2 abstract classes: Configuration and Room. Both of those have 2 concrete implementations for Coworking and Meeting (CoworkingRoomConfiguration, MeetingRoomConfiguration, MeetingRoom and CoworkingRoom).

Both are configured to have a discriminator column using Fluent API:

Room:

builder.ToTable(nameof(Room));
builder.HasKey(x => x.Id);
builder.HasDiscriminator<string>("Type")
    .HasValue<MeetingRoom>("Meeting")
    .HasValue<CoworkingRoom>("Coworking");

Configuration:

builder.ToTable(nameof(Configuration));
builder.HasKey(x => x.Id);
builder.HasDiscriminator<string>("Room_type")
    .HasValue<MeetingRoomConfiguration>("Meeting")
    .HasValue<CoworkingRoomConfiguration>("Cowork");

In my domain, a CoworkingRoom has one CoworkingRoomConfiguration. A MeetingRoom has many MeetingRoomConfigurations, though. Is it possible to explicitly declare these relationships using Fluent API, while keeping the types configured as TPH?

Right now I'm relying on EF Core inferring the relationships based on property types, but this isn't something I'd like to rely on.

Classes are below, the Entity type is based on this article

Room classes:

public abstract class Room : Entity
{
    // Omitted for brevity
}

public class MeetingRoom : Room
{
    public ICollection<MeetingRoomConfiguration> Configurations { get; }
    // Rest omitted for brevity
}

public class CoworkingRoom : Room
{
    public CoworkingRoomConfiguration Configuration { get; set; }
    // Rest omitted for brevity
}

Configuration classes:

public abstract class Configuration : Entity
{
    // Omitted for brevity
}

public class CoworkingRoomConfiguration : Configuration
{
    // Omitted for brevity
}

public class MeetingRoomConfiguration : Configuration
{
    // I'd like to get rid of the circular reference here too if possible
    public IEnumerable<MeetingRoom> MeetingRooms { get; set; }
    // Rest omitted for brevity
}
0

There are 0 answers