C#, EF code-first: Table-Per-Type (TPT) with partial FK in PK

35 views Asked by At

I like to setup EF (6) code first with a table per type setup. The requirement is that part of the PK in one inherited abstract type (see "TrackedEntity") is a FK to another abstract type (see "Entity"). Is that possible? If so, how can I do this with fluent API?

Model:

public abstract class Entity { public Guid Id { get; set; } }
public abstract class TrackedEntity : Entity { public int Version { get; set; } }

Configurations:

public class EntityConfiguration : EntityTypeConfiguration<Entity>
{
    public EntityConfiguration()
    {
        this.ToTable("Entity");
        this.HasKey(x => x.Id);
        this.Property(x => x.Id).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

public class TrackedEntityConfiguration : EntityTypeConfiguration<TrackedEntity>
{
    public TrackedEntityConfiguration()
    {
        this.ToTable("TrackedEntity");
        this.HasKey(y => new { y.Id, y.Version });
    }
}
0

There are 0 answers