2

Following code generates only single table "CertificateEvent".

How do I achieve TPT inheritance in EF Core 2.0?

public abstract class CertificateEvent {
   public int CertificateEventId { get; set; }
}

public class Assignment : CertificateEvent {...}
public class Assessment : CertificateEvent {...}

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {

    }

    public DbSet<Assessment> AssessorAssessments { get; set; }
    public DbSet<Assignment> AssessorAssignments { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<CertificateEvent>().ToTable(nameof(CertificateEvent));
        modelBuilder.Entity<Assessment>().ToTable(nameof(Assessment));
        modelBuilder.Entity<Assignment>().ToTable(nameof(Assignment));
    }
}

class MyDesignTimeDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
    public MyDbContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<MyDbContext>();
        builder.UseSqlServer("Server=(local);Database=Test;Trusted_Connection=True;MultipleActiveResultSets=true");
        return new MyDbContext(builder.Options);
    }
}

I've also tried dotnet ef migrations add Inheritance, but it did not created TPT inheritance in the database

1 Answer 1

2

TPT is not in EF Core (yet). See

The feeling from our team is that TPT is generally an anti-pattern and results in significant performance issues later on. While enabling it may make some folks "happier" to start with it ultimately just leads to issues. We are willing to consider it though, so we're leaving this open and will consider it based on the feedback we get.

https://github.com/aspnet/EntityFrameworkCore/issues/2266

Sign up to request clarification or add additional context in comments.

12 Comments

In case CertificateEvent was concrete class, would it be possible to write TPC?
No. Currently TPH only is supported, and for code-first scenarios it is also recommended. TPH was implemented first because it's the best strategy for most new applications.
The only problem I have is that I will have too many inherited classes and I don't want to end up with table with hundred columns . I will need to select all CertificateEvents, so separate tables without relationship is not possible.
@DavidBrowne-Microsoft Our team is converting a WPF app to asp.net core mvc. The WPF app uses TPT. In order to get things done quickly we are using the composition provided when the entity classes are generated. I just updated the entity classes to mirror legacies'. I'm about to try to generate a new db schema via new entity classes thus giving us a TPH structure. The ? I'm currently working on(in my head) is what are the advantages of composition over inheritance and vice/versa in this situation. I'll probably figure it out later but dropped that ? here in case you could answer it easily.
If I understand you, you are creating an EF Core model that maps to the existing tables used in EF6 TPT. This will result in a model that uses composition instead of inheritance. You will loose some of the coding convenience that the inheritance model provided. Eg Customer.Person.Name instead of Customer.Name, etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.