Entity Framework Core TPT inheritance - multiple commonly used tables

251 views Asked by At

I am currently struggling with modelling the following scenario with Entity Framework Core.

I have created the following entities using the table-per-type approach to have my Equipment and Event tables refer to the same Documents table.

public class Document 
{
    public long ID { get; set; }
    // [...]
    public long DocumentConatiningEntityId { get; set; }
    public DocumentContainingEntity DocumentContainingEntity { get; set; }
}

public class DocumentContainingEntity 
{
    public long ID { get; set; }
    public ICollection<Document> Documents { get; set; }
}

[Table("Events")]
public class Event : DocumentContainingEntity
{
    // [...]
}

[Table("Equipments")]
public class Equipment : DocumentContainingEntity
{
    // [...]
}

This works fine for only ONE common table, like in this case:

  • Equipment has n documents
  • Event has n documents

What would be the best way to handle it when I want my Equipment to refer to another table Images, which should also refer to another entity (e.g. Location)?

So:

  • Event should have n documents, but not any images
  • Equipment should have n documents and n images
  • Location should not have any documents, but n images

What I tried is changing my base entity to something more generic like SharedPrimaryKeyEntity, which should provide a common primary key base for my Locations, Events and Equipments. Then I created interfaces which provide the navigation properties

public class SharedPrimaryKeyEntity 
{
    public long ID { get; set; }
}

[Table("Events")]
public class Event 
    : SharedPrimaryKeyEntity, IDocumentEntity
{
    public ICollection<Document> Documents { get; set; } 
}

[Table("Equipments")]
public class Equipment
    : SharedPrimaryKeyEntity, IDocumentEntity, IImageEntity
{
    public ICollection<Image> Images { get; set; }
    public ICollection<Document> Documents { get; set; } 
}

[Table("Locations")]
public class Location
    : SharedPrimaryKeyEntity, IImageEntity
{
    public ICollection<Image> Images { get; set; } 
}

public class Document 
{
    public long ID { get; set; }
    // [...]
    public long SharedPrimaryKeyEntityId { get; set; }
    public SharedPrimaryKeyEntity SharedPrimaryKeyEntity { get; set; }
}

public class Image 
{
    public long ID { get; set; }
    // [...]
    public long SharedPrimaryKeyEntityId { get; set; }
    public SharedPrimaryKeyEntity SharedPrimaryKeyEntity { get; set; }
}

public interface IDocumentEntity 
{
    ICollection<Document> Documents { get; set; }
}

public interface IImageEntity 
{
    ICollection<Image> Images { get; set; }
}

But I fail to configure the foreign key relation with that approach because the base class does not know the navigation properties. And I can only refer to "SharedPrimaryKeyEntity" from my "Image"- and "Document"-Entity because otherwise i'd have to create a separate column for each referenced entity type, which I want to avoid.

Is that event possible in Entity Framework Core? Or is there any better approach to handle this case?

0

There are 0 answers