我有定义为 TPC 的 EF Core 实体。
实体:
public class Customer
{
public long Id { get; set; }
public ICollection<ProformaInvoice> ProformaInvoices { get; set; }
public ICollection<Invoice> Invoices { get; set; }
}
public class Invoice
{
public long Id { get; set; }
public double Value { get; set; }
public Customer Customer { get; set; }
}
public class ProformaInvoice : Invoice // configured as TPC
{
public string Notes { get; set; }
}
上下文:
public class MyContext : DbContext
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
var customerBuilder = builder.Entity<Customer>();
customerBuilder.HasKey(x => x.Id);
customerBuilder.HasMany(x => x.ProformaInvoices).WithOne(x => x.Customer).OnDelete(DeleteBehavior.Cascade);
customerBuilder.HasMany(x => x.Invoices).WithOne(x => x.Customer).OnDelete(DeleteBehavior.Cascade);
var invoiceBuilder = builder.Entity<Invoice>();
invoiceBuilder.UseTpcMappingStrategy(); // TPC
invoiceBuilder.HasKey(x => x.Id);
invoiceBuilder.Property(x => x.Value).IsRequired();
var proformaInvoiceBuilder = builder.Entity<ProformaInvoice>();
//proformaInvoiceBuilder.HasKey(x => x.Id); // do not configure key else get error "A key cannot be configured on 'ProformaInvoice' because it is a derived type. The key must be configured on the root type 'Invoice'."
proformaInvoiceBuilder.Property(x => x.Value).IsRequired();
proformaInvoiceBuilder.Property(x => x.Notes).IsRequired();
}
public DbSet<Customer> Customers => Set<Customer>();
public DbSet<ProformaInvoice> ProformaInvoices => Set<ProformaInvoice>();
public DbSet<Invoice> Invoices => Set<Invoice>();
}
创建迁移:
$ dotnet ef migrations add AddEntities
错误:
无法在“Customer.Invoices”和“Invoice.Customer”之间创建关系,因为“Customer.ProformaInvoices”和“ProformaInvoice.Customer”之间已经存在关系。导航只能参与单一关系。如果要覆盖现有关系,请先在“OnModelCreating”中的导航“Invoice.Customer”上调用“忽略”。
我该如何解决这个问题?我怀疑这与 TPC 设计有关。
(注意我无法更改为 TPH/TPT,我需要使用 TPC...这只是一个简化的最小复制。)
• 如何使用 EF Core Code First 在 Oracle DB 中使用自动递增 ID 播种数据
• 我正在尝试使用 onetoone 注释将三个实体与另一个实体映射
• EF Core DbContext 缓存结果包括具有动态全局过滤器的实体
• 带有 EF 核心的 UseLazyLoadingProxies 在添加新的子实体时加载整个列表
• EF Core,将大量大型实体添加到另一个实体的多对多关系的有效方法?
• Net Core Web Api – 如何为 SwaggerDoc 映射 Optional<T>
• 使用 EF Core 6 Cosmos DB 在谓词内部使用实体内部列表的计数进行查询的问题
• 如何获取与使用 JPA 映射的其他实体具有多对多关系的实体成员列表?
• 如果事先只知道表模式的一部分,如何从关系数据库加载实体?
• 如何复制 SQL 的 ORDER BY NEWID()?
• 在 JPA 中,如果我们有一个由一个实体映射的 OneToMany 或 ManyToMany,我们是否需要在 setter 中更新两者?