I would like to use Mapping Properties of an Entity Type to Multiple Tables in the Database (Entity Splitting) whilst as the same time using Mapping the Table-Per-Hierarchy (TPH) Inheritance, therefore my model mapping code is as follows:
modelBuilder
.Entity<Person>()
.HasKey(n => n.PersonId)
.Map(map =>
{
map.Properties(p => new { p.Name });
map.ToTable("dbo.Person");
})
.Map<Customer>(map =>
{
map.Requires("PersonType").HasValue("C");
map.Properties(p => new { p.CustomerNumber });
map.ToTable("dbo.Customer");
});
Based upon the following underlying database schema:
create table dbo.Person
(
PersonId int not null identity(1,1) primary key,
PersonType char(1) not null,
Name varchar(50) not null
)
create table dbo.Customer
(
PersonId int not null references dbo.Person (PersonId),
CustomerNumber varchar(10) not null
)
However, when the EF tries to execute my query:
ctx.People.ToList();
The following exception message is thrown:
Invalid column name 'PersonType'.
Running a SQL profile it would appear that its trying to use a predicate on the field PersonType
with value C
on the dbo.Customer
table, rather than on the dbo.Person
table where my discriminator really is.
If I use one or the other feature, i.e. only the inheritance or only the additional table mapping then it works but then I forfeit some of my requirements.
Can what I'm doing be done with the EF Fluent API?
Thanks for your time.