以下代码仅生成单个表" 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继承
答案 0 :(得分:2)
TPT尚未进入EF Core。见
我们团队的感觉是TPT通常是反模式的 导致以后出现重大性能问题。在启用它的同时 可能会让一些人“更开心”,最终只是开始 问题。我们愿意考虑它,所以我们离开了 打开并根据我们得到的反馈来考虑它。