如何在Entity Framework Core 2.0代码中首先创建每种类型继承的表?

时间:2017-09-12 15:07:42

标签: entity-framework .net-core entity-framework-core

以下代码仅生成单个表" CertificateEvent"。

如何在EF Core 2.0中实现TPT继承?

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);
    }
}

我还试过dotnet ef migrations add Inheritance,但它没有在数据库中创建TPT继承

1 个答案:

答案 0 :(得分:2)

TPT尚未进入EF Core。见

  

我们团队的感觉是TPT通常是反模式的   导致以后出现重大性能问题。在启用它的同时   可能会让一些人“更开心”,最终只是开始   问题。我们愿意考虑它,所以我们离开了   打开并根据我们得到的反馈来考虑它。

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