17
I am having problems to create an Entity Framework Code-First mapping for the following sample database schema (in SQL Server):

Every table contains a TenantId which is part of all (composite) primary and foreign keys (Multi-Tenancy).
A Company is either a Customer or a Supplier and I try to model this via Table-Per-Type (TPT) inheritance mapping:
public abstract class Company
{
public int TenantId { get; set; }
public int CompanyId { get; set; }
public int AddressId { get; set; }
public Address Address { get; set; }
}
public class Customer : Company
{
public string CustomerName { get; set; }
public int SalesPersonId { get; set; }
public Person SalesPerson { get; set; }
}
public class Supplier : Company
{
public string SupplierName { get; set; }
}
Mapping with Fluent API:
modelBuilder.Entity<Company>()
.HasKey(c => new { c.TenantId, c.CompanyId });
modelBuilder.Entity<Customer>()
.ToTable("Customers");
modelBuilder.Entity<Supplier>()
.ToTable("Suppliers");
The base table Companies has a one-to-many relationship to an Address (every company has an address, no matter if customer or supplier) and I can create a mapping for this association:
modelBuilder.Entity<Company>()
.HasRequired(c => c.Address)
.WithMany()
.HasForeignKey(c => new { c.TenantId, c.AddressId });
The foreign key is composed of one part of the primary key - the TenantId - and a separate column - the AddressId. This works.
As you can see in the database schema, from d