EF 4.3.1如何将子子类与每个类型的表映射 [英] EF 4.3.1 How to map a sub sub class with table per type

查看:391
本文介绍了EF 4.3.1如何将子子类与每个类型的表映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象基类叫Party。有几个具体的子类(公司,人员,部门)。 Party有一个名为PartyType的属性,用作鉴别符。每种类型都在其自己的表中,具有诸如

I have an abstract base class called Party. There are several concrete subclasses (Company, Person, Department). Party has a property called PartyType which is use as the discriminator. Each type is in its own table with configurations like

Map<Person>(p => p.Requires("PartyType").HasValue("Person").ToTable("People");

b $ b现在我想添加一个Person子类,如Employee。我如何映射这个?我试过了

Everything works well. Now I want to add a subclass of Person called Employee. How do I map this? I've tried

Map<Employee>(e => e.Requires("PartyType").HasValue("Employee")
   .ToTable("Employees");

但这会导致运行时错误


(43,10):error 3032:映射从行
开始的片段时出现问题43,84:EntityTypes WOL.EFData.Person,WOL.EFData.Employee正在
映射到表People中的相同行,映射条件可以是
用于区分这些类型映射到的行。

(43,10) : error 3032: Problem in mapping fragments starting at lines 43, 84:EntityTypes WOL.EFData.Person, WOL.EFData.Employee are being mapped to the same rows in table People. Mapping conditions can be used to distinguish the rows that these types are mapped to.


推荐答案

类型映射EF不需要鉴别符配置。

In table per type mapping EF does not expect a discriminator configuration.

modelBuilder.Entity<Person>().ToTable("People");
modelBuilder.Entity<Employee>().ToTable("Employees");

请参阅本文 for更多信息。

See this article for more information.

这篇关于EF 4.3.1如何将子子类与每个类型的表映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆