【发布时间】:2015-12-04 17:53:20
【问题描述】:
我想使用Mapping Properties of an Entity Type to Multiple Tables in the Database (Entity Splitting)同时使用Mapping the Table-Per-Hierarchy (TPH) Inheritance,因此我的模型映射代码如下:
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");
});
基于以下底层数据库架构:
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
)
但是,当 EF 尝试执行我的查询时:
ctx.People.ToList();
抛出以下异常消息:
Invalid column name 'PersonType'.
运行 SQL 配置文件时,它似乎试图在 dbo.Customer 表上使用值为 C 的字段 PersonType 上的谓词,而不是在我的鉴别器真正所在的 dbo.Person 表上使用谓词。
如果我使用一个或另一个功能,即仅继承或仅附加表映射,那么它可以工作,但我会放弃我的一些要求。
我正在做的事情可以用 EF Fluent API 完成吗?
感谢您的宝贵时间。
【问题讨论】:
标签: c# entity-framework inheritance ef-fluent-api tph